If I wanted to reject a request because a file is ...
# ktor
s
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
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.