RxJava2/Retrofit2 - Handling null for 204 PUT and DELETE requests
Since you have a RxJava2 and retrofit 2, you could use a more readable way of describing what it is you really want from an endpoint using a Completable
.
Completable
by design returns only onComplete()
and onError(Exception)
so you know what is going on right the moment you see it(you only care about execution not the return value) and not wonder what will be under Response<Void>
.
So your endpoint call should look like:
@DELETE(...)
Completable deleteFile(...args);
More types are supported, see the Retrofit 2 RxJava 2 Adapter page.Single
is one that stands out in terms of api calls.
You need to declare your request method in Retrofit
like:
@DELETE(...)
Call<Void> deleteFile(...args);
In RxJava your Observable
has to be typed:
@DELETE(...)
Observable<Response<Void>> deleteFile(...args);
In onNext()
or doOnNext()
you will receive the Response
normally, if the request is successful.
Having Void
will not send the response body to the converter for further deserialization. All empty-response Call
s should be typed as Void
.