Junior Frogie
Junior Frogie

Reputation: 313

Android Retrofit + Rxjava: How to get response on non200 code?

This is how my request looks like:

ApiService apiService = retrofit.create(ApiService.class);
Observable<Response<UserUpdateResponse>> response = apiService.updateUser(Utils.getHeader(), object);

response.subscribeOn(Schedulers.newThread())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(this::onSuccessUpdate,
                    this::onErr,
                    this::hideDialogLoading);

It's supposed to return 'code':'205' 'msg':'successfully update'. But when server response any code 201,202 (anything not 200) it will go to error.

Here is the Error.

java.net.ProtocolException: HTTP 205 had non-zero Content-Length: 121

So how do I prevent it from error, or how do I get error body? Thank you!.

Upvotes: 1

Views: 1374

Answers (2)

Junior Frogie
Junior Frogie

Reputation: 313

So technically, I can get response 2xx. The problem was that server response body in response code 205 that suppose to be null (https://www.rfc-editor.org/rfc/rfc7231#section-6.3.6). So after set body null on server, android side works fine.

Upvotes: 0

Kiskae
Kiskae

Reputation: 25603

HTTP response codes have a predefined definition and some have requirements that they must fullfill to be considered a valid HTTP payload. You cannot redefine what these codes mean for your application and expect well-implemented clients to accept it.

Looking specifically at HTTP 205 - Reset Content, which has the following requirement:

Since the 205 status code implies that no additional content will be provided, a server MUST NOT generate a payload in a 205 response.

Generally applications will just return HTTP 200 for all requests and include application-specific error codes in the payload. What you're doing does not make much sense.

Upvotes: 2

Related Questions