Getting Header from Response (Retrofit / OkHttp Client)

AndroidJsonJacksonRetrofitOkhttp

Android Problem Overview


I am using Retrofit with the OkHttp Client and Jackson for Json Serialization and want to get the header of the response.

I know that i can extend the OkClient and intercept it. But this comes before the deserialization process starts.

What i basically needs is to get the header alongside with the deserialized Json Object.

Android Solutions


Solution 1 - Android

With Retrofit 1.9.0, if you use the Callback asynchronous version of the interface,

@GET("/user")
void getUser(Callback<User> callback)

Then your callback will receive a Response object

    Callback<User> user = new Callback<User>() {
        @Override
        public void success(User user, Response response) {
            
        }

        @Override
        public void failure(RetrofitError error) {

        }
    }

Which has a method called getHeaders()

    Callback<User> user = new Callback<User>() {
        @Override
        public void success(User user, Response response) {
            List<Header> headerList = response.getHeaders();
            for(Header header : headerList) {
                Log.d(TAG, header.getName() + " " + header.getValue());
            }
        }

For Retrofit 2.0's interface, you can do this with Call<T>.

For Retrofit 2.0's Rx support, you can do this with Observable<Result<T>>

Solution 2 - Android

In Retrofit 2.0.0, you can get header like this:

public interface Api {
    @GET("user")
    Call<User> getUser();
}

Call<User> call = api.getUser();
call.enqueue(new Callback<User>() {
    @Override
    public void onResponse(Call<User> call, Response<User> response) {
        // get headers
        Headers headers = response.headers();
        // get header value
        String cookie = response.headers().get("Set-Cookie");
        // TODO
    }

    @Override
    public void onFailure(Call<User> call, Throwable t) {
        // TODO
    }
});

Solution 3 - Android

Much like you I wanted the headers along side of the payload. I needed access to the Etag. It takes some retro-foo, but you can do it. here's what I did. It's a dirty sample so dont take this as a best practices sample.

public static RestAdapter.Builder getRestBuilder(Context context) {
    GsonBuilder gsonBuilder = GsonBuilderUtils.getBuilder();
    Gson gson = gsonBuilder.create();
    // **
    // 1. create our own custom deserializer here
    // **
    final MyGsonConverter gsonConverter = new MyGsonConverter(gson);
    OkHttpClient httpClient = MyPersonalOkHttpFactory.getInstance().getAuthHttpClient(context);
    httpClient.networkInterceptors().add(new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Request originalRequest = chain.request();
            Response response = chain.proceed(originalRequest);
            // **
            // 2. add the headers from the Interceptor to our deserializer instance
            // **
            gsonConverter.headers = response.headers();
            return response;
        }
    });
    RestAdapter.Builder builder = new RestAdapter.Builder()
            .setClient(new OkClient(httpClient))
            .setEndpoint(Common.getApiOriginUrl())
            .setConverter(gsonConverter);
    return builder;
}

private static class MyGsonConverter extends GsonConverter {

    private Headers headers;

    public MyGsonConverter(Gson gson) {
        super(gson);
    }

    @Override
    public Object fromBody(TypedInput body, Type type) throws ConversionException {
        Object obj =  super.fromBody(body, type);
        // **
        // 3. at this point, gson is called and you have access to headers
        // do whatever you want here. I just set it on the return object.
        // ** 
        if (obj instanceof HeadersArrayList) {
            ((HeadersArrayList)obj).setHeaders(headers);
        }
        return obj;
    }
}

public class HeadersArrayList<K> extends ArrayList<K>{

    private Headers headers;

    public Headers getHeaders() {
        return headers;
    }

    public void setHeaders(Headers headers) {
        this.headers = headers;
    }
}

// the retrofit api for reference
@GET("/api/of/my/backend/{stuff}")
HeadersArrayList<String> getSomething(@Path("stuff") String stuff);

Solution 4 - Android

First print the entire response, body, code, message, header(by logging or something else) and try to find a clue from there.

I would recommend you to read the API docs and see the type of request it is asking for.

Use Postman to check which one of the following is working: 1.form-data 2.x-www-form-Urlencoded 3.raw 4.binary

And then accordingly set the annotations in the method declarations in the interface.

eg: in my case, it was taking x-www-form-Urlencoded so I had to mention it using

@FormUrlEncoded @Headers("Content-Type: application/x-www-form-urlencoded")

in the method declaration.

Then used @Field annotations for individual value I was sending like

Call<'ReturnObj'> Signup(@Field("name") String name, @Field("phoneNumber") long phoneNumber, @Field("password") String password, @Field("counter") int counter);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondknaackView Question on Stackoverflow
Solution 1 - AndroidEpicPandaForceView Answer on Stackoverflow
Solution 2 - AndroidSpark.BaoView Answer on Stackoverflow
Solution 3 - AndroidwhizzleView Answer on Stackoverflow
Solution 4 - AndroidDawView Answer on Stackoverflow