Reputation: 2268
{
"vUserList": [
{
"firstName": "Kate",
"lastName": "Bell",
"email": "[email protected]"
},
{
"lastName": "Higgins",
"firstName": "Daniel",
"email": "[email protected]"
},
{
"email": "[email protected]",
"firstName": "John",
"lastName": "Appleseed"
},
{
"lastName": "Haro",
"firstName": "Anna",
"email": "[email protected]"
},
{
"email": "[email protected]",
"firstName": "Hank",
"lastName": "Zakroff"
}
]
}
This is the request parameters which I need to send to the server.
func getContactsParameters() -> [String : Any] {
return [Keys.vUserList : arrContacts]
}
var headers = [
Keys.XAPIKEY: "5dik8fo5yecc25bfcc562724dd674bde5fh5ju8y"
]
Alamofire.request("urlString", method: .post, parameters: getContactsParameters(), encoding: JSONEncoding.default, headers: headers).responseJSON { response in
print(response)
}
getContactsParameters() console log is as below,
In response, I am getting Null with the Success code.
I talked with backend people, they are saying, if I don't receive this key "vUserList" then you may get null response. But, I double-checked the spelling and everything. Another scenario is that, they are saying if we don't get request in proper json then you may get null response.
I tried many different solutions, none of them are working. :(
Any help is appreciated!
Here I am attaching, postman screenshot. In postman it is working fine.
Upvotes: 1
Views: 1349
Reputation: 714
Try to pass the JSON string object of your contact array into your API params.
let arrOfUserList : [[String : AnyObject]] = [
["firstName":"Kate" as AnyObject,"lastName":"Bell" as AnyObject,"email":"[email protected]" as AnyObject],
["firstName":"Daniel" as AnyObject,"lastName":"Higgins" as AnyObject,"email":"[email protected]" as AnyObject]
]
do {
let data = try JSONSerialization.data(withJSONObject: arrOfUserList, options: [])
let jsonString = NSString(data: data, encoding: String.Encoding.utf8.rawValue)
if jsonString != nil {
let paramsContact = [Keys.vUserList : jsonString]
//Do your API calling code here
}
} catch {
print(error.localizedDescription)
}
Hope it will work for you.
Upvotes: 2
Reputation: 16466
After Check your Postman
Please Add Header in your request
let headers = [
"Content-Type": "application/x-www-form-urlencoded"
]
If it doesn't work then
URLEncoding()
or URLEncoding.httpBody
or URLEncoding.default
at the place of JSONEncoding.default
Upvotes: 1