Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 @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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading