title: Basic Authentication with Retrofit
body: <p>I am trying to build a client for a REST API using Retrofit. The API uses basic auth and I have been unable to authenticate using Retrofit.</p>
  
  <p>I tested the API using the curl below and it works as expected</p>
  
  <pre><code>curl  -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{some_json}' -u api_key: https://apitest.com/api/v1/customers
  </code></pre>
  
  <p>Below is the Retrofit client</p>
  
  <pre><code>public interface UserService {
  
  String HOST = "https://apitest.com";
  
  public static OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
  public static Retrofit.Builder builder =
          new Retrofit.Builder()
                  .baseUrl(HOST)
                  .addConverterFactory(GsonConverterFactory.create());
  /*
   * CREATE/UPDATE User
   */
  @POST("api/v1/customers")
  Call&lt;UserAPIResponse&gt; userUpdate(@Body UserUpdateRequest userUpdateRequest);
  
  
  
  static UserService newInstance(String userAPIKey) {
      String credentials = userAPIKey + ":";
  
      final String basic = "Basic "+ Base64.encodeBase64(credentials.getBytes());
  
      httpClient.addInterceptor(new Interceptor() {
          @Override
          public Response intercept(Interceptor.Chain chain) throws IOException {
              Request original = chain.request();
  
              Request.Builder requestBuilder = original.newBuilder()
                      .header("Authorization", basic);
              requestBuilder.header("Accept", "application/json");
              requestBuilder.method(original.method(),original.body());
  
  
              Request request = requestBuilder.build();
              return chain.proceed(request);
          }
      });
  
  OkHttpClient client = httpClient.build();
  Retrofit retrofit = builder.client(client).build();
  
  return retrofit.create(BlueshiftUserService.class);
  }
  </code></pre>
  
  <p>When I call updateUser on the UserService</p>
  
  <pre><code>Response&lt;UserAPIResponse&gt; response = UserService.userUpdate(userUpdateRequest).execute();
  </code></pre>
  
  <p>The response.code is 401 (unauthorized/authentication failed)</p>
  
  <p>The curl command with -u and the same credentials works as expected. </p>