title: Okhttp Authenticator addHeader() vs header()
body: <p>While using an Interceptor and Authenticator to add an access token header, my Authenticator correctly gets called when the server responds with a 401.</p>
  
  <p>The Authenticator renews the access token via a separate network call and sets it in the header to try again. The code would be very similar <a href="https://www.coderdump.net/2018/04/automatic-refresh-api-token-with-retrofit-and-okhttp-authenticator.html" rel="nofollow noreferrer">to this sample</a></p>
  
  <p>I just spent an hour figuring out I used the wrong method to add the renewed token to the authenticator, <a href="https://square.github.io/okhttp/3.x/okhttp/okhttp3/Request.Builder.html" rel="nofollow noreferrer"><code>addHeader</code> vs <code>header</code></a>.</p>
  
  <p>Works:</p>
  
  <pre><code>if (accessToken != null){
     // retry the failed 401 request with new access token
     return response.request().newBuilder()
                              .header("Authorization", accessToken) // use the new access token
                              .build();
  }
  </code></pre>
  
  <p>Doesn't work:</p>
  
  <pre><code>if (accessToken != null){
     // retry the failed 401 request with new access token
     return response.request().newBuilder()
                              .addHeader("Authorization", accessToken) // use the new access token
                              .build();
  }
  </code></pre>
  
  <p>What could be going on here and why does <code>addHeader()</code> not behave the same as <code>header()</code></p>