Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrofit-2 Content-Type Issue

My Api is accepting Content-Type application/json as headers. I set Header perfectly as mentioned in Retrofit Docs.

@Headers("Content-Type: application/json")
@POST("user/classes")
Call<playlist> addToPlaylist(@Body PlaylistParm parm);

But in Request Log it is Returning Content-Type txt/html.So how i should fix this issue? This api works fine in POSTMAN enter image description here

enter image description here

like image 877
Muhammad Zubair Ashraf Avatar asked Mar 02 '16 12:03

Muhammad Zubair Ashraf


4 Answers

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

like image 198
Avinash Verma Avatar answered Oct 06 '22 15:10

Avinash Verma


you can try one in below my solutions :

@POST("user/classes")
Call<playlist> addToPlaylist(@Header("Content-Type") String content_type, @Body PlaylistParm parm);

then call

mAPI.addToPlayList("application/json", playListParam);

Or

Create HttpClient object

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();
like image 29
ThaiPD Avatar answered Oct 06 '22 15:10

ThaiPD


Here's a solution that works as of retrofit2(v2.5) and okhttp3. Works especially well if you will add these headers to a number of requests

OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder();
        httpClientBuilder.addInterceptor(new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                Request.Builder requestBuilder = chain.request().newBuilder();
                requestBuilder.header("Content-Type", "application/json");
                requestBuilder.header("Accept", "application/json");
                return chain.proceed(requestBuilder.build());
            }
        });

 OkHttpClient httpClient = httpClientBuilder.build();
 Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .client(httpClient)
                .build();
like image 8
Mitch M Avatar answered Oct 06 '22 15:10

Mitch M


Try:

@POST("user/classes")
Call<playlist> addToPlaylist(
@Header("Content-Type") String contentType,
@Body PlaylistParm parm);
like image 3
Anurag Chutani Avatar answered Oct 06 '22 17:10

Anurag Chutani