Skip to content

Instantly share code, notes, and snippets.

@voghDev
Last active August 22, 2018 08:44
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save voghDev/35c79194ecc85e1cb95738b60d9b4b1c to your computer and use it in GitHub Desktop.
Save voghDev/35c79194ecc85e1cb95738b60d9b4b1c to your computer and use it in GitHub Desktop.
Retrofit2 interceptor to add headers to HTTP requests
public class AddHeaderInterceptor implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
Request.Builder builder = chain.request().newBuilder();
builder.addHeader("Authorization", "headerContent");
return chain.proceed(builder.build());
}
}
@voghDev
Copy link
Author

voghDev commented Aug 11, 2016

Usage

    OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
    httpClient.addNetworkInterceptor(new AddHeaderInterceptor());
    httpClient.addInterceptor(new LogJsonInterceptor());
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(getEndPoint())
            .addConverterFactory(GsonConverterFactory.create())
            .client(httpClient.build())
            .build();

@soonsam123
Copy link

What is the difference between addNetworkInterpector() and addInterceptor() ?

@psiska
Copy link

psiska commented Aug 22, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment