简体   繁体   中英

Retrofit - delete was null but response body type was declared as non-null

So, After I delete a specific topic in my API it throws to me this error message

deleteTopic was null but response body type was declared as non-null

The topic is succefully deleted but in my client, this response resolves in my catch block somehow

Service

@DELETE("topics/{id}")
    suspend fun deleteTopic(@Path("id") id:String)

Repo

override suspend fun deleteTopic(id: String): Resource<Unit> {
        return Resource.Success(RetrofitClient.webservice.deleteTopic(id))
    }

ViewModel

fun deleteTopic(id:String) = liveData(Dispatchers.IO) {
        emit(Resource.Loading())
        try {
            emit(repo.deleteTopic(id))
        }catch (e:Exception){
            emit(Resource.Failure(e))
        }
    }

For some reason I succefully delete the topic in my server, but when it returns to my client, it goes to the catch and returns this exception that I mentioned above, the response is a 204 not content, but how can I fix this response to sucefully tell me that this operation has done correctly?

I'm confused, I have readed into github that I must retunr Response<Unit> but I'm still confused why I should do this, it also says taht returning that will prevent my 4xx return codes to be thrown

I think the problem is that your deletion endpoint does return empty body response (just { }). If you can, change your backend source to not do so. Otherwise, you can prob add a custom converter to handle 4xx status codes properly.

https://github.com/square/retrofit/issues/1554#issuecomment-178633697

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM