Alamofire Content-Type application / json for the PUT method

advertisements

I'm now finding solution for add content-type: application/json in Alamofire Content-Type application/json for PUT method. I've developed as follow, but not working correctly.

        Alamofire.request(.PUT, Config.preferenceURL, parameters: param, headers: headers)
            .validate(contentType: ["application/json"])
            .responseJSON { response in
                let swiftyJsonVar = JSON(response.result.value!)
                print(swiftyJsonVar)
                if (swiftyJsonVar["success"]) {

                }

                JHProgressHUD.sharedHUD.hide()
        }


You need to specify encoding: .JSON in your request method. Otherwise it will encode your parameters as query parameters in the URL. By using .JSON encoding, the Content-Type header will be set for you automatically.

Alamofire.request(.PUT, Config.preferenceURL, parameters: param, encoding: .JSON, headers: headers)

You can also use the debugPrint API to print out the cURL command equivalent to the request you're sending to the server.

let request = Alamofire.request(.PUT, Config.preferenceURL, parameters: param, encoding: .JSON, headers: headers)
debugPrint(request)

Cheers.