I have a post request done with Ktor this way
val response = httpClient.post("${BASE_URL}/following") {
contentType(ContentType.Application.Json)
setBody(UnfollowRequestBody(userId, "delete"))
}
With the class being
@Serializable
data class UnfollowRequestBody(
@SerialName("user_id") val userId: Int?,
@SerialName("_method") val method: String = "delete",
)
When this call is logged
METHOD: POST
BODY Content-Type: application/json
BODY START
{"user_id":28367}
BODY END
the second parameter is omitted, which causes an API error as without the _method
being set to "delete" it treats the request as a follow request, instead of an unfollow one.
Any idea on how to force the second parameter to be sent as well?
I found the answer. By default, Ktor doesn't encode default parameters.
So, for this, there are 2 possible solutions:
add encodeDefaults = true
in the Ktor client configuration
Remove the = "delete"
from the data class definition