Swift HMRC VAT MTD: Retrieve VAT obligations

Back to Index

Retrieve VAT obligations

Documentation: https://developer.service.hmrc.gov.uk/api-documentation/docs/api/service/vat-api/1.0#_retrieve-vat-obligations_get_accordion

CURL Command

curl -X GET  https://test-api.service.hmrc.gov.uk/organisations/vat/MY_HMRC_VRN/obligations?from=2018-06-25&to=2019-01-01 \
-H 'Authorization: Bearer HMRC_ACCESS_TOKEN' \
-H 'Accept: application/vnd.hmrc.1.0+json'

Swift Example


func chilkatTest() {
    let rest = CkoRest()
    var success: Bool

    //  URL: https://test-api.service.hmrc.gov.uk/organisations/vat/MY_HMRC_VRN/obligations?from=2018-06-25&to=2019-01-01
    var bTls: Bool = true
    var port: Int = 443
    var bAutoReconnect: Bool = true
    success = rest.Connect("test-api.service.hmrc.gov.uk", port: port, tls: bTls, autoReconnect: bAutoReconnect)
    if success != true {
        print("ConnectFailReason: \(rest.ConnectFailReason.intValue)")
        print("\(rest.LastErrorText)")
        return
    }

    rest.AddHeader("Authorization", value: "Bearer HMRC_ACCESS_TOKEN")
    rest.AddHeader("Accept", value: "application/vnd.hmrc.1.0+json")

    let sbResponseBody = CkoStringBuilder()
    success = rest.FullRequestNoBodySb("GET", uriPath: "/organisations/vat/MY_HMRC_VRN/obligations?from=2018-06-25&to=2019-01-01", sb: sbResponseBody)
    if success != true {
        print("\(rest.LastErrorText)")
        return
    }

    var respStatusCode: Int = rest.ResponseStatusCode.intValue
    if respStatusCode >= 400 {
        print("Response Status Code = \(respStatusCode)")
        print("Response Header:")
        print("\(rest.ResponseHeader)")
        print("Response Body:")
        print("\(sbResponseBody.GetAsString())")
        return
    }

    let jsonResponse = CkoJsonObject()
    jsonResponse.LoadSb(sbResponseBody)

    //  See the Online Tool for Generating JSON Parse Code
    var i: Int
    var count_i: Int
    var start: String?
    var end: String?
    var due: String?
    var status: String?
    var periodKey: String?
    var received: String?

    i = 0
    count_i = jsonResponse.SizeOfArray("obligations").intValue
    while i < count_i {
        jsonResponse.I = i
        start = jsonResponse.StringOf("obligations[i].start")
        end = jsonResponse.StringOf("obligations[i].end")
        due = jsonResponse.StringOf("obligations[i].due")
        status = jsonResponse.StringOf("obligations[i].status")
        periodKey = jsonResponse.StringOf("obligations[i].periodKey")
        received = jsonResponse.StringOf("obligations[i].received")
        i = i + 1
    }


}

Sample JSON Response Body

{
  "obligations": [
    {
      "start": "2017-01-01",
      "end": "2017-03-31",
      "due": "2017-05-07",
      "status": "F",
      "periodKey": "18A1",
      "received": "2017-05-06"
    },
    {
      "start": "2017-04-01",
      "end": "2017-06-30",
      "due": "2017-08-07",
      "status": "O",
      "periodKey": "18A2"
    }
  ]
}