Astro throws “Cannot use Astro.rewrite() after the request body has been read” when a POST, PUT, PATCH or DELETE request’s body was already consumed, for example by await Astro.request.formData(), and the code then calls Astro.rewrite() or context.rewrite(). A rewrite forwards the original request, including its body, to another route, and a body can only be read once. Read the body from Astro.request.clone() instead.
Key Takeaways
- Only requests that can carry a body (not GET or HEAD) hit this error.
formData(),json(),text()andarrayBuffer()all consume the body.- Clone first:
await Astro.request.clone().formData()leaves the original readable.- In middleware, the same rule applies to
context.requestbeforecontext.rewrite().
The exact error
Cannot use `Astro.rewrite()` after the request body has been read.
`Astro.rewrite()` cannot be used if the request body has already been read. If you need to read the body, first clone the request.
Hint: Read the body from `Astro.request.clone()` instead of `Astro.request`, so the original request can still be rewritten.
The hint line was added in Astro 7.4.
What causes it
When you rewrite, Astro creates a new Request for the target URL and passes the original body stream along with it. If that stream has already been read, there’s nothing to pass, and the Fetch API marks the request with bodyUsed. Astro checks this and stops:
---
// src/pages/contact.astro (broken)
export const prerender = false;
if (Astro.request.method === 'POST') {
const data = await Astro.request.formData();
if (!data.get('email')) {
return Astro.rewrite('/contact-error/');
}
}
---
The fix
Read from a clone, so the original body is still available to the rewrite target:
---
// src/pages/contact.astro
export const prerender = false;
if (Astro.request.method === 'POST') {
const data = await Astro.request.clone().formData();
if (!data.get('email')) {
return Astro.rewrite('/contact-error/');
}
}
---
<form method="post">
<input type="email" name="email" required />
<button>Send</button>
</form>
The same fix in middleware:
// src/middleware.ts
import { defineMiddleware } from 'astro:middleware';
export const onRequest = defineMiddleware(async (context, next) => {
if (context.request.method === 'POST' && context.url.pathname === '/signup/') {
const body = await context.request.clone().json();
if (!body.plan) return context.rewrite('/signup/choose-plan/');
}
return next();
});
When you don’t need a rewrite
If the target page doesn’t need the submitted body, a redirect is often the better tool. Astro.redirect('/contact-error/') with its default 302 status sends the browser to a new URL as a GET request, so the body isn’t forwarded and this error can’t happen. Redirecting after a POST also stops the browser from resubmitting the form on refresh.
Another option is to keep the logic in one place. Render the error state in the same page instead of rewriting, or use Astro Actions, which parse the form for you and return results to the page.
Rewrites and form handling need on-demand rendering; see Astro hybrid rendering for switching single routes, and no adapter installed if the build refuses server routes. Komal themes in the catalog build to static files, so adding a server-rendered form route is an opt-in change; our static deploy docs cover hosting the rest. The official entry is in the Astro error reference.
FAQ
What is the difference between Astro.rewrite() and Astro.redirect()?
Astro.rewrite() renders another route at the same URL and forwards the original request. Astro.redirect() tells the browser to request a new URL, as a fresh GET without the original body.
Can I read the request body twice in Astro?
Not from the same Request. Call Astro.request.clone() before reading, and read each clone once.
Does this error happen on GET requests?
No. GET and HEAD requests have no body, so Astro skips the check and the rewrite works.