2

I have set up a local express app to monitor requests made by my iOS app and found it odd that when printing the headers, one entry is

'accept-language': 'en-US,de;q=0.9'

because I did not set any specific headers at all. I tried to set nil for that header like

var request = URLRequest(url: URL(string: "http://localhost:3000/iosnativereq")!)
request.setValue(nil, forHTTPHeaderField: "Accept-Language") //also tried lower case

let task = URLSession.shared.dataTask(with: request) { data, response, error in
    print(data, response, error)
}

task.resume()

however the requests my server gets still include the field. I would prefer having control over what headers to be sent, how can I make iOS not add these headers (or at least be able to remove them before actually performing the task)

user2330482
  • 1,053
  • 2
  • 11
  • 19
  • 1
    [This question](https://stackoverflow.com/questions/39627616/preventing-nsurlsession-default-http-headers) might be informative (although probably not what you want to hear :)). – Joakim Danielson May 17 '23 at 17:45
  • 1
    I don't think you can remove default headers even if you try to use a custom URLSession. But did you try to set it to `*` (i.e. `Accept-Language: *` so that it can accept any language? If that doesn't work, and you really must get rid of them... don't use URLSession is the only answer unfortunately... – timbre timbre May 17 '23 at 17:46
  • Thank you for you guys' comments. I'm not running into specific issues with the headers (yet), I just found it odd that they get added regardless of what I tried. – user2330482 May 17 '23 at 18:36
  • It's possible to remove the value, although not the key (eg. `"Accept-Language": ""`). – l'L'l May 17 '23 at 20:40

0 Answers0