2

My app creates POST request with Authorization header - it looks like :

let accessToken = "veryCoolToken"
var request = URLRequest(url: myURL)
request.httpMethod = "POST" 
request.addValue("application/json", forHTTPHeaderField: "content-type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
request.setValue("token=\"\(accessToken)\"", forHTTPHeaderField: "Authorization")

I already checked this request with Postman app - it was fine. But for some reason I received needed result only from Postman and not from my app.

So after taking apart request on my Server - I found that when my app sending POST request - it is not "Authorization" but "authorization".

Why is that happening?

What else should I take in consideration when app sends POST request with headers?

Dávid Pásztor
  • 40,247
  • 8
  • 59
  • 80
moonvader
  • 13,845
  • 14
  • 51
  • 93

1 Answers1

2

For POST requests in Swift, generally you have to set the following:

request.setValue("Basic " + accessToken, forHTTPHeaderField: "Authorization")
Pranav Kasetti
  • 4,773
  • 2
  • 16
  • 38
  • Are you sure that this is the reason why of my issue? – moonvader Sep 24 '17 at 20:26
  • All POST requests for basic authentication have authorisation headers in this format in swift. You could try replacing the `token=...` line with this to test it out. – Pranav Kasetti Sep 24 '17 at 20:46
  • I have problem not with value of Authorization header. I have problem with "Authorization" sended in lowercase variant – moonvader Sep 25 '17 at 10:45
  • Directly setting the `Authorization` is not recommended, as it is a reserved header within iOS. https://developer.apple.com/documentation/foundation/nsurlrequest – Alex Chase Jan 26 '21 at 07:36