title: How to set a 'Token xxxxxxxxxx' for 'Authorization' in HTTP header in URLSession
body: <p>The backend APIs I'm working with require a token to be sent with every request against the HTTP header key <code>Authorization</code>  in this format - <code>Token xxxxxxxxxx</code>.</p>
  
  <p>Right now, I'm doing the following.</p>
  
  <pre><code>var getRequest = URLRequest(url: url)
  getRequest.addValue("Token xxxxxxxx", forHTTPHeaderField: "Authorization")
  </code></pre>
  
  <p>This works sometimes and some other times, the header field <code>Authorization</code> is stripped when the request is sent out. I checked this using Charles proxy.</p>
  
  <p><a href="https://developer.apple.com/documentation/foundation/nsurlsessionconfiguration/1411532-httpadditionalheaders?language=objc" rel="noreferrer">Apple's documentation</a> states the following.</p>
  
  <blockquote>
    <p>An NSURLSession object is designed to handle various aspects of the
    HTTP protocol for you. As a result, you should not modify the
    following headers: Authorization, Connection, Host, WWW-Authenticate</p>
  </blockquote>
  
  <p>As a solution to this, many suggest using the <code>didReceiveAuthenticationChallenge</code> delegate method for URLSession.</p>
  
  <p>Here, you need to pass a <code>URLSession.AuthChallengeDisposition</code> instance to tell how you want to respond to the challenge and a <code>URLCredential</code> instance to pass the credentials to respond to the authenticate challenge.</p>
  
  <p>I do not know how to and if I can create a <code>URLCredential</code> instance that will add <code>Token xxxxxxxx</code> for the header field <code>Authorization</code>.</p>
  
  <p>Can somebody more knowledgeable please help me how to go about solving this?</p>
  
  <p>PS - All code mentioned in this question is in Swift 3.
  <a href="https://stackoverflow.com/questions/20920302/nsurlsession-with-token-authentication">This question</a> asks something similar to what I have. But, the answers given there don't work for me. And, some of the questions asked under the questions regarding Apple not allowing headers for <code>Authorization</code> to be added have gone unanswered.</p>
  
  <p>Edit:</p>
  
  <p>Posting relevant code.</p>
  
  <pre><code>var getRequest = URLRequest(url: url)
  getRequest.httpMethod = "GET"
  getRequest.addValue("application/json", forHTTPHeaderField: "Content-Type")
  getRequest.addValue("application/json", forHTTPHeaderField: "Accept")
  let token = DataProvider.sharedInstance.token
  getRequest.addValue("Token \(token)", forHTTPHeaderField: "Authorization")
  let getTask = URLSession.shared.dataTask(with: getRequest) { (data, response, error) in
          if let data = data {
              print("--------GET REQUEST RESPONSE START--------")
              print("CODE: \((response as? HTTPURLResponse)?.statusCode ?? 0)")
              print("Response Data:")
              print(String(data: data, encoding: .utf8) ?? "")
              print("--------GET REQUEST RESPONSE END--------")
          }
  }
  getTask.resume()
  </code></pre>
  
  <p>Here, I can confirm that the header field for 'Authorization' is getting added to the request's header dictionary.</p>
  
  <p>But, when I check what request hits the server, the header field for 'Authorization' is missing. Any thoughts?</p>