0

I want to send to the API only the data that I indicate in my data class.

data class UserRequestDTO(
    val name: String = "",
    val surname: String = ""
)

My empty class

val userDTO = UserRequestDTO()
MyService.getService().users()

My request

@POST("/vo/search")
    fun users(
        @Body userRequestDTO: UserRequestDTO
    ): Call<ResponseDTO>

But the following json is being sent:

{"name": "", "surname": ""}

How can I have a class in which I only send the data that I fill in? don't want any data to be sent, and if for example I fill in the name, I don't want the last name to be sent

1 Answer 1

3

Making them optional should result in not sending them:

data class UserRequestDTO(
    val name: String? = null,
    val surname: String? = null
)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much, it works. I was using class Builder and it works fine, but your option is better

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.