Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add multiple headers with ok Http

I am using Retrofit 2 and Okhttp for my android project. I want to add multiple headers in the api request.

This is my interceptor code :

public class NetworkInterceptors implements Interceptor {
@Override
public Response intercept(Interceptor.Chain chain) throws IOException {

    Request request = chain.request().newBuilder()
            .addHeader("Userid", "10034")
            .addHeader("Securitykey", "Fb47Gi")
            .build();
    return chain.proceed(request);
    }
}

This is not working properly. In server side I am getting only the last added header (in the above example I am getting only Securitykey missing "Userid" )

Please Help.

like image 950
Bikesh M Avatar asked Mar 01 '16 16:03

Bikesh M


People also ask

How do I add a header in OkHttp?

Adds a header with name and value. Prefer this method for multiply-valued headers like "Cookie". Note that for some headers including Content-Length and Content-Encoding, OkHttp may replace value with a header derived from the request body.

What is interceptor in OkHttp?

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.


1 Answers

Thanks for support I found the answer, This is working fine for me

public class NetworkInterceptors implements Interceptor {
    @Override
    public Response intercept(Interceptor.Chain chain) throws IOException {

        Request request = chain.request();
        Request newRequest;

        newRequest = request.newBuilder()
                .addHeader("Userid", "10034")
                .addHeader("Securitykey", "Fb47Gi")
                .build();
        return chain.proceed(newRequest);
    }
}
like image 99
Bikesh M Avatar answered Oct 03 '22 05:10

Bikesh M