Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

soonsam123 commented Aug 21, 2018

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