androidresthttpretrofit400-bad-request

Sending enum via PUT method, Retrofit | Error 400


I want to send enum class to API via Retrofit. For parsing I'm using Gson.

Enum class that I'm sending:

enum class TaskState {
NOT_ASSIGNED,
IN_PROGRESS,
CLOSED,
DELETED }

My Retrofit method:

 @PUT("/project/76fd04cc-ae1e-4589-8fe7-11c4f16ce067/task/{taskId}/state/")
suspend fun updateState(
    @Path("taskId") taskId: String,
    @Body state: TaskState
)

Unfortunately, when I invoke this function, I receive an HTTP 400 error. The headers, project ID, task ID, and TaskState are all 100% correct and in accordance with the API documentation. Other methods described in the documentation work. Below is the link do API's docs. Method that I want to use is described last.

enter image description here

Link to the docs: https://task-manager-api-401408.lm.r.appspot.com/docs/#/default/editTaskState

Where is the problem?


Solution

  • You need to send the enum value as a string. It doesn't know that it is a body enum.

    Sample Usage

    data class TaskStateBody(val state: String)
    

    ..

    @Body state: TaskStateBody
    

    ...

    val stateBody = TaskStateBody(TaskState.CLOSED.name)