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 ?