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

This comment has been minimized.

Copy link
Owner Author

@voghDev 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

This comment has been minimized.

Copy link

@soonsam123 soonsam123 commented Aug 21, 2018

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

@psiska

This comment has been minimized.

Copy link

@psiska psiska commented Aug 22, 2018

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