bill
bill

Reputation: 864

how to send a dictionary as a parameter in Alamofire, swift 3

I want to send below parameter

{"serial_number":{"type":"match", "value":"somevalue"}}

so I tried like this

 let parameters : [String:AnyObject] = [
            "serial_number":[
                "type":"match",
                "value":serialNum
            ]
        ]

and 'GET' like this

Alamofire.request(.GET, Constant.WebClient.Cool + "/creator/" + cid + "/wa?filling=", parameters: nil, headers: headers)
            .responseJSON { response in

                switch response.result {
                case.Success(let JSON):
                    print("get json is success")
                    if JSON["status"] != nil && JSON["status"] as! String == "fail"{
                        print("some error occured")
                    }
                    else{
                        let resultArray = JSON as! NSArray
                        print("\(resultArray)")
                    }
                    break
                case .Failure(let error):
                    print("request failed with error : \(error.localizedDescription)")

                }

        }

but didn't work for me. how to get

{"serial_number":{"type":"match", "value":"someval"}}

to a dictionary. hope your help with this. when I try to print the dictionary, it was like below

["serial_number": {
    type = match;
    value = "some val";
}]

hope your help with this.

Upvotes: 3

Views: 1510

Answers (1)

rjndra
rjndra

Reputation: 121

The alamofire request uses default encoding i.e. URLEncoding.default which in this case doesn't work as expected, so you have to change the encoding to JSONEncoding(options: [])

Follow documentation : https://github.com/Alamofire/Alamofire#json-encoding

Upvotes: 1

Related Questions