Reputation: 31
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.
Upvotes: 0
Views: 180
Reputation: 1035
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
}
}
}
}
Upvotes: 1
Reputation: 1253
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.
Upvotes: 0