2

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": s@gmail.com, "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 .

Sunita
  • 336
  • 2
  • 18

2 Answers2

1

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

Community
  • 1
  • 1
ipraba
  • 16,485
  • 4
  • 59
  • 58
0

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)
  • Thanks Aseem.... I already tried this solution, but as I said I am using Alamofire to make network calls, so Alamofire only accepts params in Dictionary only. There is no way to pass params as Dictionary. Is there any other way to solve this problem? – Sunita Aug 18 '16 at 14:56