ralphgabb
ralphgabb

Reputation: 10528

Retrofit Coroutine Status Code 205 Body Null

I'm having difficulty on getting the response data from server.

I have a validation scenario that server throws an error if a user already exist, somehow someway, the api developer decided to use 205 as status code instead of usual 200.

The problem is everytime I'm calling the API, the body() and errorBody() returns null As suggested, I used the Response to get the response.

isSuccessful() is returning true, I can see on my logcat the raw Json response from server but return both null on body() and errorBody(), any idea what seems to be the error here?

Thanks In Advance.

override suspend fun safeRegisterAccount(registerBody: RegisterBody): LiveData<out Wrapper<RegisterResponse>?> {
        val result = MutableLiveData<Wrapper<RegisterResponse>>()
        try {
            val account = networkService.safeRegister(registerBody)
            val wrapper = Wrapper<Token>()  
            wrapper.objectData = account.body()?.objectData
            wrapper.status = account.code()
            result.postValue(wrapper)
        } catch (ex: Exception) {
            plantLog(ex.message)
        }

        return result
    }

Upvotes: 0

Views: 1000

Answers (1)

Saurabh Thorat
Saurabh Thorat

Reputation: 20684

Retrofit skips the converter if the status code is 204 or 205.

You can try adding an OkHttp Interceptor which would convert the server's 205 code to 200 before Retrofit works on it.

Like this:

class BodyInterceptor : Interceptor {
    override fun intercept(chain: Interceptor.Chain): Response {
        val response = chain.proceed(chain.request())

        if (response.code == 204 || response.code == 205) {
            return response
                .newBuilder()
                .code(200)
                .body(response.body)
                .build()
        } else {
            return response
        }
    }
}

Upvotes: 3

Related Questions