Articles

Set a different upload size limit for each API route

Set per-route request body limits at the Gregale edge and verify a 413 on one endpoint while the same file passes another.

Updated

Put the upload limit on the route, not across the whole API. On Gregale, a limit edge rule can allow a 256 KiB batch import while rejecting an avatar request over 64 KiB. A request over the active cap gets HTTP 413 request_too_large. You can change the rule without a new deployment.

Download the runnable example. It uses two routes on the same disposable app. It doesn't store the uploads.

1. Give each endpoint its own limit

These rules apply only to POST requests. Use exact paths so another API route doesn't inherit a limit you didn't intend.

sh
gregale edge rule create --app my-api --kind limit --match-host @app \
  --match-path /avatar --match-method POST --limit-max-body-bytes 65536

gregale edge rule create --app my-api --kind limit --match-host @app \
  --match-path /import --match-method POST --limit-max-body-bytes 262144

The values are in bytes, and the cap applies to the whole request body. A 64 KiB file inside multipart form data can exceed a 64 KiB body limit because the form adds bytes. If you use multipart, leave overhead in the edge limit and keep a separate file size check in the app.

These commands set the buffered-body cap. If the app uses streaming, check the streaming body setting and platform limits as well. A reverse proxy or framework with a lower limit can still reject the request earlier. A Gregale rule doesn't raise those limits.

2. Test the same bytes on both routes

The example uses fixed-length binary bodies. The same 128 KiB file must get 413 on /avatar and 200 on /import. The receiving app returns the byte count and SHA-256 so an empty response handler or a truncated body won't look like a successful upload.

Request bodyRouteExpected with the rules
64 KiB/avatar200, exact bytes
128 KiB/avatar413
128 KiB/import200, exact bytes
256 KiB + 1/import413

To inspect a rejection yourself, use a synthetic file and curl:

sh
curl -sS -i -H 'Content-Type: application/octet-stream' \
  --data-binary @synthetic.bin https://YOUR-APP.gregale.app/avatar

Inspect the response body as well as the status. The drill checks Gregale's request_too_large problem and the app's own receipt separately. It also sends oversized bodies before the rules are set, then checks that the same original deployment is still at 100% afterward.

3. Run the drill on a disposable app

Install the Gregale CLI, Bash, curl, jq and OpenSSL. Export a test workspace key as GREGALE_TOKEN, extract the download and run bash run-demo.sh. It reserves one new public app with synthetic responses and verifies its removal on exit. It doesn't change a custom domain or register an external service.

If your intended uploads exceed the request limits of your full path, use direct uploads to private storage rather than trying to make an application endpoint accept an arbitrarily large body. Keep content and authorization checks in the app or upload flow; a size limit doesn't validate a file.

Questions

Can two API routes have different upload size limits?

Yes. Create a Gregale limit edge rule for each route and method. Set each request body limit in bytes, without redeploying the app.

Why does a file smaller than the limit still return HTTP 413?

A body limit includes multipart form overhead, not just the file. A smaller cap at another proxy, the hosting plan or the application can also cause a 413.

Does an upload size limit validate file content?

No. It limits body size. Keep file type, authorization and content checks in your application or direct upload flow.