Im trying to call a service via POST that sends an e-mail to the user, the body always return empty, I have to deal with the response code. e.g. 204 = success.
Im trying to deal this way, but im not succeeding
Service:
@POST("xxxxxx")
fun resendBankSlip(@Path("userId") userId: Int): Deferred<Response>
ViewModel:
scope.launch {
try {
_loading.value = true
val response = repository.sendEmail(userId)
if (!response.isSuccessful) {
_error.value = R.string.generic_error_message
}
} catch (e: Throwable) {
_error.value = R.string.generic_error_message
} finally {
_loading.value = false
}
}
The error happens on val response = repository.sendEmail(userId)
Exception:
java.lang.IllegalArgumentException: 'okhttp3.Response' is not a valid response body type. Did you mean ResponseBody?
for method EmailService.sendEmail
Any idea?
You probably confused okhttp3.Response
with retrofit.Response
. Try to use retrofit2.Response
wrapper in API response like that:
@POST("xxxxxx")
fun resendBankSlip(@Path("userId") userId: Int): Deferred<retrofit2.Response<Unit>>
After that you can easily get response code via response.code()
.
Also note that I passed Unit
as Response
's type argument because you don't need the body. In other cases you should pass an actual type of response body.