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.