Reputation: 336
I am trying to send Dictionary in POST API. For this purpose I am using Alamofire.
Parameter Dictionary is as follows:
var param: Dictionary<String, AnyObject> =
[
"name" : name,
"email" : email,
"mobile_number" : mobile_number,
"body" : body
] as Dictionary<String, AnyObject>
param["listing_ids[]"] = [772121,772136,772129]
Sending request to Server is as follows:
Alamofire.request(.POST,baseUrl+enquiryEndPoint , parameters: params , encoding: .URL, headers: createHeader()).responseJSON { response in
switch response.result {
case .Success:
guard let response = response.result.value as? Dictionary<String, AnyObject> else{return}
completionBlock(response,error: nil)
case .Failure(let error):
completionBlock([:],error: error)
}
}
When Printing parameter dictionary , error is as follows:
["mobile_number": 457823356, "email": [email protected], "body": Hello, I found your property on <country-website>. Please send me additional information about this property.
Thank you, "listing_ids[]": <_TtCs21_SwiftDeferredNSArray 0x1400d6ff0>(
708783,
589915,
722173,
746261,
618410
)
, "name": s]
Value corresponding to key "listing_ids[]" is an array of Int. This is causing problem here.
Something "_TtCs21_SwiftDeferredNSArray" is written over there which is totally unclear to me.
Due to which I am getting blank response.
Please help me .
Upvotes: 2
Views: 2545
Reputation: 121
AnyObject can only represent class type. In Swift, Array and Dictionary are struct, instead of the class type in many other languages. The struct cannot be described into AnyObject, and this is why Any comes in. Besides of class, Any can be utilized in all other types too, including struct and enum.
Therefore you have to change the Dictionary to Dictionary. Below code is working fine and printing dictionary as per your requirement.
var param: Dictionary<String, Any> = [
"name" : "name",
"email" : "email",
"mobile_number" : 123132,
"body" : "Hello"
]
param["listing_ids[]"] = [772121,772136,772129]
print(param)
Upvotes: 0
Reputation: 16543
As mentioned in this post may be you can try changing the encoding for your request
Alamofire.request(.POST,baseUrl+enquiryEndPoint , parameters: params , encoding: .URL, headers: createHeader()).responseJSON { response in
to
Alamofire.request(.POST,baseUrl+enquiryEndPoint , parameters: params , encoding: .JSON, headers: createHeader()).responseJSON { response in
Note it is just the encoding parameter of the method changed
Upvotes: 1