Skip to content

Instantly share code, notes, and snippets.

@lfmingo
Created March 30, 2016 18:56
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lfmingo/768a29eafc4a9d25d75ef31e2be506f7 to your computer and use it in GitHub Desktop.
Save lfmingo/768a29eafc4a9d25d75ef31e2be506f7 to your computer and use it in GitHub Desktop.
Add headers in http request using okhttp interceptor with retrofit 2
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.addInterceptor(new Interceptor() {
@Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Request original = chain.request();
Request request = original.newBuilder()
.header("User-Agent", "Your-App-Name")
.header("Accept", "application/vnd.yourapi.v1.full+json")
.method(original.method(), original.body())
.build();
return chain.proceed(request);
}
}
OkHttpClient client = httpClient.build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(API_BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();
@anos19
Copy link

anos19 commented Dec 9, 2020

hello this code request return ..what is code return it??

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