httpAdditionalHeaders not working on linux

痴心易碎 提交于 2020-01-04 03:50:40

问题


I ran into a problem of different behaviour of URLSession / URLSessionConfiguration / URLSessionDataTask on OS X and Linux.

Swift: 3.0.2 Kitura: 1.3

I am doing following:

let aURL = URL(string: "...")!

// Because shared is not implemented                
let sessionConfig = URLSessionConfiguration.default

sessionConfig.httpAdditionalHeaders = ["Accept": "application/json", "Accept-Language": "sv-SE"]

let session = URLSession(configuration: sessionConfig)

// additionalHeaders are set just fine                
Log.info("\(session.configuration.httpAdditionalHeaders)")

let dataTask = session.dataTask(with: aURL, completionHandler: { data, loadResponse, error in
                   ...
                })

 dataTask.resume()

The additional headers are set on the configuration object, but when deployed to Bluemix the response show that language header field is missing (i get the response in wrong language).

I know that the request is correct because when I build and run this (Kitura) locally (thorough Xcode on OS X) I get the expected behaviour.

Has anyone encountered this? What to do? Where to go?


回答1:


This could've been a comment, but I am still not allowed to post comments!

Yes, my colleague came across this while working on this bug. I think the work-around that you adopted is the best alternative option. This needs more investigation. I have created a new bug report report for this issue.




回答2:


Found a hackaround myself doing:

...
var request = URLRequest(url: aURL, cachePolicy: .reloadRevalidatingCacheData, timeoutInterval: 3)
request.setValue("sv-SE", forHTTPHeaderField: "Accept-Language")
let dataTask = URLSession(configuration: URLSessionConfiguration.default).dataTask(with: request, ...

Even though it works it would be nice to know what is going on with the original solution, because I like it better (it's prettier).

Is it a bug in Foundation on linux?

Edit: Workaround seems to only work in Swift 3.0.2, it doesn't work in 3.0.1. Something is really funky with URL headers.



来源:https://stackoverflow.com/questions/41229814/httpadditionalheaders-not-working-on-linux

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!