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

spierce7

08/12/2022, 8:07 PM
If I wanted to reject a request because a file is too large, without downloading the entire file on my server, what’s the best way to do that? Will something like the code in the thread handle that?
👀 1
Copy code
val contentLength: Long = call.request.header(HttpHeaders.ContentLength)
    ?.toLongOrNull()
    ?.let { if (it <= BYTES_3_MB) it else null }
    ?: run {
        call.respond(HttpStatusCode.BadRequest)
        return@post
    }
a

Aleksei Tirman [JB]

08/16/2022, 9:52 AM
Yes, it should work. Also, when a client doesn’t send the
Content-Length
header you can accumulate number of bytes read while receiving a request body to verify that it doesn’t exceeds a limit.