https://kotlinlang.org logo
Title
m

mmaillot

11/25/2020, 9:35 PM
Hi! How can I send a 204 No content response without content ?
call.respond(HttpStatusCode.NoContent, Unit)
I have
{}
in the content.
c

carrot

11/25/2020, 11:58 PM
I don’t think you need the second parameter there, HttpStatusCode can be sent by itself for no content
m

Milan Hruban

11/26/2020, 8:31 AM
☝️ How? because there is either
ApplicationCall.respond(status: HttpStatusCode, message: Any)
or
ApplicationCall.respond(message: Any)
if you send only HttpStatusCode, the second method will be used and the status will be serialized as response content
m

mmaillot

11/26/2020, 9:10 AM
Yes, the
respond
method doesn’t exist with only the HTTP code as parameter.
m

Matteo Mirk

11/26/2020, 9:15 AM
call.response.status(HttpStatusCode.NoContent)
that’s it, you just access the response object and set values which will be immediately sent to the client. https://ktor.io/docs/responses.html
c

carrot

11/26/2020, 10:05 AM
Unless I'm missing something entirely, it does work with
call.respond(HttpStatusCode.NoContent)
directly - there'll be a feature somewhere checking if
message
has a type of
HttpStatusCode
and mutating the response. I'll have a dig later 🙂