How to handle "204 no content" response with Retrofit after logout?

1.2k views Asked by At

In my Android app, I have the following logout function:

suspend fun logout(token: String): NetworkResult<Unit> {
      return try{
          val logoutResponse = service.logout("Token $token")
          NetworkResult.Success(logoutResponse)
      }catch(e:Throwable){
          NetworkResult.Error(null)        // <-- after a successful logout, we proceeed with this line WHY ? 
      }
}

The API interface containing the endpoints contains this:

@POST("logout")
suspend fun logout(@Header("Authorization") token: String)

On the server side, I use django-rest-knox as 3rd party library for authentication. The logout endpoint of that library has a built-in LogoutView which returns "204 no content" response after a successful logout. The documentation recommends to not alter the LogoutView since modifications would lead to unpredictable results. But although the logout request was successful on the server-side, Retrofit considers the "204 no content" response as an exception. As I mentioned in the code snippet above, the catch clause is executed.

Now to my question(s): When the request is successful, why does Retrofit throws an exception? How to deal with "204 no content" responses at all ?

0

There are 0 answers