Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add Api_KEY into interceptor using okhttp

I have this service where I want to put the token as an interception in the okhttp instead of passing as a parameter with @Header("MY_API_KEY")

This is my code regarding the service

/**
     * Provides the [PHService]
     */
    fun provideService(): PHService {

        val logger = HttpLoggingInterceptor()
        logger.level = HttpLoggingInterceptor.Level.BASIC



        val client = OkHttpClient.Builder()
                .addInterceptor(logger)
                .build()

        return Retrofit.Builder()
                .baseUrl(BuildConfig.API_URL)
                .client(client)
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(PHService::class.java)
    }

How can I add an interceptor for header authorization in here?

like image 338
José Nobre Avatar asked Aug 14 '18 00:08

José Nobre


People also ask

How does OkHttp interceptor work?

Interceptors, according to the documentation, are a powerful mechanism that can monitor, rewrite, and retry the API call. So, when we make an API call, we can either monitor it or perform some tasks.

What is OkHttp logging interceptor?

OkHttp is an interceptor which helps you to log your API calls.

How do I use OkHttp with retrofit?

Solution 1: Sharing Default OkHttp Instance In order to make them share a single OkHttp instance, you can simply pass it explicitly on the builder: OkHttpClient okHttpClient = new OkHttpClient(); Retrofit retrofitApiV1 = new Retrofit. Builder() . baseUrl("https://futurestud.io/v1/") .

What is OkHttp interceptor Android?

Interceptors are a powerful mechanism that can monitor, rewrite, and retry calls. Here's a simple interceptor that logs the outgoing request and the incoming response. class LoggingInterceptor implements Interceptor { @Override public Response intercept(Interceptor.


1 Answers

add like this

HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
    logging.setLevel(HttpLoggingInterceptor.Level.BODY);

    OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
    httpClient.addInterceptor(new Interceptor() {
        @Override
        public Response intercept(Interceptor.Chain chain) throws IOException {
            Request original = chain.request();

            // Request customization: add request headers
            Request.Builder requestBuilder = original.newBuilder()
                    .header("Authorization", "MY_API_KEY"); // <-- this is the important line

            Request request = requestBuilder.build();
            return chain.proceed(request);
        }
    });


    httpClient.connectTimeout(30, TimeUnit.SECONDS);
    httpClient.readTimeout(30, TimeUnit.SECONDS);
    httpClient.addNetworkInterceptor(logging);

 OkHttpClient client = httpClient.build();

in kotlin its like

 val logging = HttpLoggingInterceptor()
    logging.level = HttpLoggingInterceptor.Level.BODY

    val httpClient = OkHttpClient.Builder()
    httpClient.addInterceptor { chain ->
        val original = chain.request()

        // Request customization: add request headers
        val requestBuilder = original.newBuilder()
                .header("Authorization", "MY_API_KEY") // <-- this is the important line

        val request = requestBuilder.build()
        chain.proceed(request)
    }


    httpClient.connectTimeout(30, TimeUnit.SECONDS)
    httpClient.readTimeout(30, TimeUnit.SECONDS)

    httpClient.addNetworkInterceptor(logging)

 val okHttpClient=httpClient.build()
like image 107
Devil10 Avatar answered Oct 24 '22 13:10

Devil10