Retrofit: Content-Type header removed by retrofit 2.1 again

Created on 27 Jul 2016  路  1Comment  路  Source: square/retrofit

Hey, As per the doc I tried setting Headers through request interceptors first. Below is the snippet

OkHttpClient httpClient = new OkHttpClient.Builder()
.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Request requestWithHeader = request.newBuilder().header("Authorization",RequestInterceptor.authToken)
.header("Content-Type","application/json")
.method(request.method(),request.body())
.build();

                    return chain.proceed(requestWithHeader);
                }
            }).addNetworkInterceptor(httpLoggingInterceptor)
    .build();

When i checked the logs, content-type is missing.

  1. I tried setting headers in the method through @Header, @HeaderMap. Nothing positive and Content-Type being removed automatically.

Code snippet:

@GET("/categories/")
Call<Result> getCategory(@Header("Content-Type") String header);

//Invocation part:
Call call1 = new RestClient().getApiService().getCategory("application/json");

Most helpful comment

OkHttp removes the content type when the request method (in this case GET) does not support a body: https://github.com/square/okhttp/blob/6014ab9752cff344a0baf57eb7626d98ca1d3401/okhttp/src/main/java/okhttp3/internal/http/RetryAndFollowUpInterceptor.java#L317. Retrofit does not control this behavior since it's deep inside OkHttp, but I agree with it. A Content-Type header makes no sense when there's no actual request body with content.

>All comments

OkHttp removes the content type when the request method (in this case GET) does not support a body: https://github.com/square/okhttp/blob/6014ab9752cff344a0baf57eb7626d98ca1d3401/okhttp/src/main/java/okhttp3/internal/http/RetryAndFollowUpInterceptor.java#L317. Retrofit does not control this behavior since it's deep inside OkHttp, but I agree with it. A Content-Type header makes no sense when there's no actual request body with content.

Was this page helpful?
0 / 5 - 0 ratings