POST bad params with NSURLSession

Hi,


I try to use NSURLSession with HTTPAdditionalHeader


My code setting headers in request


let URL = NSURL(string: "\(hostName)\(uri)")!
let request = NSMutableURLRequest(URL: URL)
request.HTTPMethod = method
request.HTTPBody = NSJSONSerialization.dataWithJSONObject(params, options: nil, error: nil)

request.setValue("application/vnd.myapp.v2", forHTTPHeaderField: "Accept")
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue("gzip", forHTTPHeaderField: "Content-Encoding")


And with POST request it's produce this on my server :



Parameters: {"email"=>"me@email.com", "password"=>"[FILTERED]", "subdomain"=>"api", "session"=>{"email"=>"me@email.com"", "password"=>"[FILTERED]"}}


With NSURLSession, setting headers with HTTPAdditionalHeaders of NSURLSessionConfiguration


var headers = [
        "Accept": "application/vnd.myapp.v2",
        "Content-Type": "application/json",
        "Content-Encoding": "gzip",
]


 let request = NSMutableURLRequest(URL: NSURL(string: "\(hostName)\(uri)")!)
request.HTTPMethod = method
 request.HTTPBody = NSJSONSerialization.dataWithJSONObject(params, options: nil, error: nil)
 let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
configuration.HTTPAdditionalHeaders = headers
let session = NSURLSession(configuration: configuration, delegate: nil, delegateQueue: NSOperationQueue.mainQueue())

And with POST request it's produce this on my server :



Parameters: {"{\"email\":\"me@email.com",\"password\":\"passwordfield\"}"=>"[FILTERED]", "subdomain"=>"api"}


In fact, the Content-Type is overwrited with NSURLSession with : application/x-www-form-urlencoded


Juste aksing what the diffrence between setting headers in request or in NSURLSessionConfiguration ?

Answered by QuinceyMorris in 7600022

It's a known bug in 10.10.3. As you've found, the workaround is to set the content-type header directly in the request. If you want to submit an additional bug report, you can say it's a duplicate of #20340623.

Accepted Answer

It's a known bug in 10.10.3. As you've found, the workaround is to set the content-type header directly in the request. If you want to submit an additional bug report, you can say it's a duplicate of #20340623.

POST bad params with NSURLSession
 
 
Q