How to handle 204 response in Retrofit using Kotlin Coroutines?

You can use Response<Unit> to handle 204 responses using Retrofit but when you handle like this Retrofit will not throw an exception for 4xx responses or other exceptional cases.

You may deal with these cases with the following code:

return try {
        val result = apiCall.invoke()
        return if (result is Response<*>) {
            if (result.code() >= 400) {
                // create an error from error body and return
            } else {
                // return success result
            }
        } else {
            // directly return success result
        }
    } catch (t: Throwable) {
        // create an error from throwable and return it!
    }

According to this, use Response<Unit> in the retrofit method declaration.

retrofit no content issue