Recently I started using Retrofit 2
and I faced an issue with parsing empty response body. I have a server which responds only with http code without any content inside the response body.
How can I handle only meta information about server response (headers, status code etc)?
Thanks in advance!
Here is how I used it with Rx2 and Retrofit2, with PUT REST request: My request had a json body but just http response code with empty body.
The Api client:
The interface:
Then to use it:
Edit:
As Jake Wharton points out,
is the best way to go versus my original response --
You can just return a
ResponseBody
, which will bypass parsing the response.Then in your call,
If you use RxJava, then it's better to use
Completable
in this casehttp://reactivex.io/RxJava/2.x/javadoc/io/reactivex/Completable.html
in the accepted answer:
If the endpoint returns failure response code, it will still be in the
onNext
and you will have to check the response code yourself.However, if you use
Completable
.you will have only
onComplete
andonError
. if the response code is success it will fire theonComplete
else it will fireonError
.If you are using rxjava, use something like :