# Reject requests in HTTP.jl server based on memory usage

**URL:** <https://discourse.julialang.org/t/reject-requests-in-http-jl-server-based-on-memory-usage/99216>\
**Category:** Web Stack\
**Created:** [May 22, 2023, 12:38pm UTC](https://discourse.julialang.org/t/reject-requests-in-http-jl-server-based-on-memory-usage/99216 "2023-05-22T12:38:12Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![quinnj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/quinnj/32/11_2.png) [@quinnj](https://discourse.julialang.org/u/quinnj)\
**Post date:** [May 22, 2023, 9:31pm UTC](https://discourse.julialang.org/t/reject-requests-in-http-jl-server-based-on-memory-usage/99216/5 "2023-05-22T21:31:16Z")

</div>

Just chiming in to say that you certainly could use the “middleware” approach to handle a scenario where a client tries to send a request w/ like, a 10GB request body. The general approach here would be:

- Define a _streaming_ middleware (which has the form `f(::HTTP.Stream) -> Nothing`, instead of the higher-level _request_ middleware which has the form `f(::Request) -> Response)`)
- In the streaming middleware, you would call `startread(::Stream)`, which returns once the _headers_ have been received on the request
- Then check the `Content-Length` header to see if the request body is going to be over some max limit you’ve set for your server
- If the request body _is_ too large, you can reject and close the stream immediately (returning a 413 is traditional), to ensure the actual large body isn’t read into memory or allowed to affect the server.

I’ve wanted to include something like this in HTTP as a sort of “default middleware” people could use, but haven’t gotten around to it. If someone wants to pick it up and make a PR, I’d be happy to review/merge.

---

_[View the full topic](https://discourse.julialang.org/t/reject-requests-in-http-jl-server-based-on-memory-usage/99216)._
