https://kotlinlang.org logo
#ktor
Title
# ktor
m

Marcello Galhardo

05/16/2022, 5:19 PM
Hey folks, I have a question about Ktor Http Client (Android). I have a
post
request where the body is always the same:
Copy code
@Serializable
internal data class IgnoreRequest(val status: String = "IGNORE")
And I send it like this:
Copy code
<http://httpClient.post|httpClient.post><Unit>(urlString = "whatever/${id.value}") { body = IgnoreRequest() }
The idea is that you are updating the status of item to “IGNORE”. However, it looks like Ktor simple “does not send the body” when you have only default parameters:
Copy code
19:06:37.052  I  HttpClient: CONTENT HEADERS
19:06:37.052  I  HttpClient: -> Content-Length: 3
19:06:37.055  I  HttpClient: -> Content-Type: application/json; charset=utf-8
19:06:37.055  I  HttpClient: BODY Content-Type: application/json; charset=utf-8
19:06:37.056  I  HttpClient: BODY START
19:06:37.056  I  HttpClient: {
19:06:37.056  I  }
19:06:37.056  I  HttpClient: BODY END
That feels wrong. The parameter is not optional (nullable). It simply has a default parameter where for this particular case won’t change… and Ktor is not sending anything (API fails for missing body). Here is my question: is the Ktor’s behaviour by design? If yes, do you mind elaborating a little or letting me know where I can find details? If no, should I open a bug ticket for that? Thanks.
a

Aleksei Tirman [JB]

05/16/2022, 5:21 PM
This article should help you.
m

Marcello Galhardo

05/16/2022, 5:22 PM
Thanks @Aleksei Tirman [JB]. I will check it out! 🙏
2 Views