ticofab
ticofab

Reputation: 7717

Retrofit, callback for 204 No Content response?

On Android, I initially implemented a Retrofit interface like this:

@DELETE(USER_API_BASE_URL + "/{id}")
public void deleteUser(@Path("id") String id, Callback<User> callback);

The server returns 204 NO CONTENT upon a successful deletion. This was causing the callback to trigger failure, with retrofit.RetrofitError: End of input at character 0 of, as it was expecting a User object back with the response.

I then rewrote it like this, using Void instead of User:

@DELETE(USER_API_BASE_URL + "/{id}")
public void deleteUser(@Path("id") String id, Callback<Void> callback);  <-- VOID

But I am getting the same error from the callback. What is the proper way to fix this? Thank you.

Upvotes: 20

Views: 16234

Answers (3)

flame3
flame3

Reputation: 2992

This is the kotlin way for the implementation to deal with HTTP 204 and no content.

@DELETE(USER_API_BASE_URL + "/{id}")
suspend fun deleteuser(@HeaderMap headers: Map<String, String>, 
                       @Path("id") id: String)
: Response<Void>

Upvotes: 1

ticofab
ticofab

Reputation: 7717

The solution was pointed out by Jake Wharton in the comments. Use ResponseCallback.

EDIT: this response is no longer valid for Retrofit < 2.

Upvotes: 9

Will Vanderhoef
Will Vanderhoef

Reputation: 808

Retrofit 2.x no longer has a ResponseCallback as mentioned in the other answer. You want to use a Response<Void> type.

The RxJava declaration:

@PUT Observable<Response<Void>> foo();

The standard declaration:

@PUT Call<Response<Void>> bar();

Upvotes: 32

Related Questions