Batool
Batool

Reputation: 61

Android Retrofit 2.0.0-beta2 Post string body dynamic headers

Retrofit 2.0.0-beta2

@Headers({
            "Authorization: {authorization}",
            "Content-Type: application/json"
    })
    @POST("/api/{id}/action/")
    Call<String> postfn(@Header("Authorization") String authorization, @Path("id") String id, @Body String body);

i am using Gson converter .addConverterFactory(GsonConverterFactory.create(gson))

i am getting error code=400, message=Bad Request Should i use a custom converter?

Please help

Upvotes: 1

Views: 1612

Answers (2)

Batool
Batool

Reputation: 61

I moved back to the stable version 1.9 and intercepting worked fine.

Upvotes: 0

Rodrigo Henriques
Rodrigo Henriques

Reputation: 1812

You can't put this two things together.

There are two ways to put dynamic headers on requests with retrofit 2.0

1: put it only in method signature

@Headers({
    "Content-Type: application/json"
})
@POST("/api/{id}/action/")
Call<String> postfn(@Header("Authorization") String authorization, @Path("id") String id, @Body String body);

2: using request interceptor to add fixed dynamic headers

public class TraktvApiProvider implements Provider<TraktvApi> {

    public static final String BASE_URL = "https://api-v2launch.trakt.tv/";

    @Override
    public TraktvApi get() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(JacksonConverterFactory.create())
                .build();

        retrofit.client().interceptors().add(new LoggingInterceptor());

        return retrofit.create(TraktvApi.class);
    }

    private class LoggingInterceptor implements Interceptor {
        @Override
        public Response intercept(Chain chain) throws IOException {

            Request request = chain.request();

            request = request.newBuilder()
                    .addHeader("trakt-api-version", "2")
                    .addHeader("trakt-api-key", "[YOUR-API-KEY]")
                    .build();

            Response response = chain.proceed(request);

            String bodyString = response.body().string();

            Log.d("Retrofit", "---------------------------------- REQUEST ----------------------------------");
            Log.d("Retrofit", String.format("%s - %s", request.method(), request.url()));
            Log.d("Retrofit", request.headers().toString());
            Log.d("Retrofit", "---------------------------------- REQUEST ----------------------------------");
            Log.d("Retrofit", "---------------------------------- RESPONSE ----------------------------------");
            Log.d("Retrofit", response.headers().toString());
            Log.d("Retrofit", "Body: " + bodyString);
            Log.d("Retrofit", "---------------------------------- RESPONSE ----------------------------------");

            return response.newBuilder()
                    .body(ResponseBody.create(response.body().contentType(), bodyString))
                    .build();
        }
    }
}

Upvotes: 2

Related Questions