How to use interceptor to add Headers in Retrofit?

How to use interceptor to add Headers in Retrofit?

You can use an interceptor in Retrofit to add custom headers to outgoing HTTP requests. Interceptors in Retrofit allow you to modify the request before it is sent to the server. Here's how you can add headers using an interceptor:

First, create an OkHttpClient with the interceptor and then use it when building your Retrofit instance.

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class RetrofitExample {
    public static void main(String[] args) {
        // Define your custom interceptor to add headers
        Interceptor headerInterceptor = chain -> {
            Request originalRequest = chain.request();
            Request.Builder builder = originalRequest.newBuilder()
                    .header("Authorization", "Bearer YourAccessToken")
                    .header("Custom-Header", "Custom-Value");

            Request newRequest = builder.build();
            return chain.proceed(newRequest);
        };

        // Create an OkHttpClient with the interceptor
        OkHttpClient client = new OkHttpClient.Builder()
                .addInterceptor(headerInterceptor)
                .build();

        // Create a Retrofit instance with the OkHttpClient
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://api.example.com/") // Replace with your base URL
                .client(client)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        // Create a Retrofit service interface
        MyApiService apiService = retrofit.create(MyApiService.class);

        // Use apiService to make API requests with the added headers
        // ...
    }
}

In this example:

  1. We define an Interceptor called headerInterceptor. Inside the interceptor, we create a new Request with the headers you want to add. Replace "Authorization" and "Custom-Header" with the headers you need, and "YourAccessToken" and "Custom-Value" with their respective values.

  2. We create an OkHttpClient with the interceptor added to it.

  3. We create a Retrofit instance and use the custom OkHttpClient with it.

  4. Finally, we create a Retrofit service interface (MyApiService in this example) and use it to make API requests. The added headers will be automatically included in these requests.

Make sure to replace "https://api.example.com/" with your actual API base URL and define your Retrofit service interface (MyApiService) with the appropriate API endpoints and request methods.


More Tags

dot-source timeoutexception android-app-bundle reload jquery-chosen excel-2007 sql-server-2008 connection-timeout quoting session-state

More Java Questions

More Animal pregnancy Calculators

More Housing Building Calculators

More Livestock Calculators

More Other animals Calculators