Search code examples
javaandroidhttpretrofit2put

Retrofit 2: 400 Bad Request when having body


I am trying to perform a status update PUT request. The following example returns 200 in Postman:

URL:

http://www.example.com/users/3/status?seId=1&dt=2016-11-01T00:00:00Z

HEADERS:

Content-Type:application/json

charset:utf-8

Authorization:Bearer LONG_TOKEN_HERE

BODY:

{ "status": 1 }

This is the structure of my Retrofit 2 request:

@PUT("users/{id}/status")
Call<Void> updateEventStatus(@Header("Authorization") String token,
                             @Path("id") int id,
                             @Query("seId") int seId,
                             @Query("dt") String dateTime,
                             @Body Status status);

The request's URL is the same as in Postman and so are the headers, so I suspect it is related to the body. Status is just a wrapping class with a single int field named status, which I created by following this answer (I did the same with credentials and it works well). I also tried making the status in body of type int but it results in Bad Request as well.

Any idea what could be the difference between the Postman request and the Retrofit 2 request? Thanks!

EDIT: This is the originalRequest in Retrofit 2:

Request{method=PUT, url=http://example.com/api/users/3/status?seId=0&dt=2016-10-04T05:30:00Z, tag=null}

headers: Authorization: Bearer LONG_TOKEN_HERE

contentType: application/json; charset=UTF-8

content:

0 = 123
1 = 34
2 = 115
3 = 116
4 = 97
5 = 116
6 = 117
7 = 115
8 = 34
9 = 58
10 = 51
11 = 125

Translated content:
{"status":3}

Solution

  • Eventually, it was a server-side bug (I received a false seId at first, and then tried to PUT using an seId that doesn't exist).