0

I am using Alamofire 4 and Swift 4. I am attempting to make an API request that is passed 1 parameter called Body which is a JSON string. The JSON string should look like this:

{
    "BirthDate": "1985-02-08",
    "GivenName": "mary",
    "FamilyName": "lee",
    "LicenceNumber": "94977000",
    "StateOfIssue": "ACT"
}

My code is returning the following result in the debug console:

{
  "result" : {
    "statuscode" : "400",
    "error" : [
      "GivenName needs to be a string",
      "FamilyName needs to be a string",
      "BirthDate needs to be a string",
      "LicenceNumber needs to be a string",
      "StateOfIssue needs to be a string",
      "MiddleName needs to be a string",
      "GivenName needs a value",
      "FamilyName needs a value",
      "BirthDate needs a value",
      "LicenceNumber needs a value",
      "StateOfIssue needs a value"
    ]
  }
}

My code is as follows:

public func testApi(){

    let headers = ["token":self.APIkey, "Content-Type": "application/x-www-form-urlencoded"]
    let url = self.testApiUrl

    let bodyString : String = "{\"BirthDate\":\"1985-02-08\",\"GivenName\":\"mary\",\"FamilyName\":\"lee\",\"LicenseNumber\":\"94977000\",\"StateOfIssue\":\"ACT\"}"
    let params : [String:String] = ["Body":"\(bodyString)"]

    Alamofire.request(url, method: .post, parameters: params, encoding: JSONEncoding.default, headers: headers)
        .responseJSON { response in

            if let apiResponse = response.result.value as? [String : AnyObject] {
                print("params is \(params)")
                if apiResponse["exceptionId"] as? String == nil {

                    print(apiResponse)

                    return
                }
            }
    }

}

Could someone please help? I have tried breaking down the Body string to a dictionary level (e.g. Body: [name:Mary ... etc]) but this did not work either, and the API docs say it should be passed 1 parameter called Body which is a string.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253

2 Answers2

1

try below code

public func testApi(){

    //replace "application/x-www-form-urlencoded" with "application/json"
    //let headers = ["token":self.APIkey, "Content-Type": "application/x-www-form-urlencoded"]

    let headers = ["token":self.APIkey, "Content-Type": "application/json"]
    let url = "https://sandbox.rapidid.com.au/dvs/driverLicence"

    let bodyString: [String: Any] = [
        "BirthDate": "1985-02-08",
        "GivenName": "mary",
        "FamilyName": "lee",
        "LicenceNumber": "94977000",
        "StateOfIssue": "ACT"
    ]
    //I think you don't want "Body" in request
    //let params : [String: Any] = ["Body": bodyString]

    //replace params with bodyString
    Alamofire.request(url, method: .post, parameters: bodyString, encoding: JSONEncoding.default, headers: headers)
        .responseJSON { response in

            if let apiResponse = response.result.value as? [String : AnyObject] {
                print("params is \(bodyString)")
                if apiResponse["exceptionId"] as? String == nil {
                    print(apiResponse)
                    return
                }
            }
    }
}
ios_dev
  • 1,025
  • 15
  • 27
0

Usually when a POST request is made by passing JSON data, the data is passed in the body.

...API docs say it should be passed 1 parameter called Body which is a string

If I understand correctly then the API is expecting JSON Data in the body of the post request.

Looks like you haven't encoded the JSON correctly. Here's how to fix it...

public func testApi() {

    let headers = ["token":self.APIkey, "Content-Type": "application/x-www-form-urlencoded"]
    let url = self.testApiUrl

    let bodyString: [String: Any] = [
        "BirthDate": "1985-02-08",
        "GivenName": "mary",
        "FamilyName": "lee",
        "LicenceNumber": "94977000",
        "StateOfIssue": "ACT"
    ]
    let params : [String: Any] = ["Body": bodyString]

    Alamofire.request(url, method: .post, parameters: params, encoding: JSONEncoding.default, headers: headers)
        .responseJSON { response in

            if let apiResponse = response.result.value as? [String : AnyObject] {
                print("params is \(params)")
                if apiResponse["exceptionId"] as? String == nil {
                    print(apiResponse)
                    return
                }
            }
    }
}

Hopefully this fixes it. However if you need to pass the JSON as a parameter then you need to encode it into the URL. Check out this answer for that.

NSAdi
  • 1,243
  • 9
  • 19
  • Thank you so much for responding. That returned the same error response. I will try looking the suggested URL encoding you have suggested – Skye Dunworth Dec 26 '17 at 02:36
  • Actually not all your params are String. Some expect value, make sure you aren't accidentally making everything String. – NSAdi Dec 26 '17 at 02:44
  • It is saying both that they all need to be a string and that they all need a value. hmm. – Skye Dunworth Dec 26 '17 at 03:13