So, if you want to print with Location, you would have to go through all the fields in
request.allHTTPHeaderFields
and format the key with uppercase.
The way this is written it seems like you’re making a suggestion for how to access the headers in their original case rather than what MS_ios is looking for, that is, a way to set the headers with a specific case.
Despite that, the technique you proposed seems to work, which was a bit of a surprise to me. Specifically, this code:
var req = URLRequest(url: URL(string: "http://sully.local:12345/")!)
req.setValue("XYZLocation", forHTTPHeaderField: "location")
req.allHTTPHeaderFields?["location2"] = "XYZLocation2"
URLSession.shared.dataTask(with: req) {
…
}.resume()
produces headers like this on the wire:
Location: XYZLocation
location2: XYZLocation2
I wasn’t expecting that that would actually work.
… you should also be using case insensitive headers in your backend.
Yes!
@MS_ios, I want to be absolutely clear here: HTTP headers are specified to be case insensitive [1] so if you’ve created a server that differentiates between
location
and
Location
then you’re asking for problems down the line. The above seems to provide a workaround on current Apple platforms but it’s a very bad idea to rely on that indefinitely. You need to fix your server.
Share and Enjoy
—
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
let myEmail = "eskimo" + "1" + "@apple.com"
[1] Specifically, see Section 3.2 of RFC 7230 which says, in its very first sentence:
Each header field consists of a case-insensitive field name followed by a colon …