Adding header to all request with Retrofit 2
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();httpClient.addInterceptor(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request().newBuilder().addHeader("parameter", "value").build(); return chain.proceed(request); }});Retrofit retrofit = new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create()).baseUrl(url).client(httpClient.build()).build();
The Latest Retrofit Version HERE -> 2.1.0.
lambda version:
builder.addInterceptor(chain -> { Request request = chain.request().newBuilder().addHeader("key", "value").build(); return chain.proceed(request); });
ugly long version:
builder.addInterceptor(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request().newBuilder().addHeader("key", "value").build(); return chain.proceed(request); } });
full version:
class Factory {public static APIService create(Context context) { OkHttpClient.Builder builder = new OkHttpClient().newBuilder(); builder.readTimeout(10, TimeUnit.SECONDS); builder.connectTimeout(5, TimeUnit.SECONDS); if (BuildConfig.DEBUG) { HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(); interceptor.setLevel(HttpLoggingInterceptor.Level.BASIC); builder.addInterceptor(interceptor); } builder.addInterceptor(chain -> { Request request = chain.request().newBuilder().addHeader("key", "value").build(); return chain.proceed(request); }); builder.addInterceptor(new UnauthorisedInterceptor(context)); OkHttpClient client = builder.build(); Retrofit retrofit = new Retrofit.Builder().baseUrl(APIService.ENDPOINT).client(client).addConverterFactory(GsonConverterFactory.create()).addCallAdapterFactory(RxJavaCallAdapterFactory.create()).build(); return retrofit.create(APIService.class); }}
gradle file (you need to add the logging interceptor if you plan to use it):
//----- Retrofit compile 'com.squareup.retrofit2:retrofit:2.1.0' compile "com.squareup.retrofit2:converter-gson:2.1.0" compile "com.squareup.retrofit2:adapter-rxjava:2.1.0" compile 'com.squareup.okhttp3:logging-interceptor:3.4.0'
Try this type header for Retrofit 1.9 and 2.0. For Json Content Type.
@Headers({"Accept: application/json"})@POST("user/classes")Call<playlist> addToPlaylist(@Body PlaylistParm parm);
You can add many more headers i.e
@Headers({ "Accept: application/json", "User-Agent: Your-App-Name", "Cache-Control: max-age=640000" })
Dynamically Add to headers:
@POST("user/classes")Call<ResponseModel> addToPlaylist(@Header("Content-Type") String content_type, @Body RequestModel req);
Call you method i.e
mAPI.addToPlayList("application/json", playListParam);
Or
Want to pass everytime then Create HttpClient object with http Interceptor:
OkHttpClient httpClient = new OkHttpClient(); httpClient.networkInterceptors().add(new Interceptor() { @Override public com.squareup.okhttp.Response intercept(Chain chain) throws IOException { Request.Builder requestBuilder = chain.request().newBuilder(); requestBuilder.header("Content-Type", "application/json"); return chain.proceed(requestBuilder.build()); } });
Then add to retrofit object
Retrofit retrofit = new Retrofit.Builder().baseUrl(BASE_URL).client(httpClient).build();
UPDATE if you are using Kotlin remove the { }
else it will not work