{"record":{"id":"71dfe809a60297be","repo":"withastro/astro","slug":"rewritewithbodyused","errorCode":"RewriteWithBodyUsed","errorMessage":"`Astro.rewrite()` cannot be used if the request body has already been read. If you need to read the body, first clone the request.","messagePattern":"`Astro\\.rewrite\\(\\)` cannot be used if the request body has already been read\\. If you need to read the body, first clone the request\\.","errorType":"exception","errorClass":"AstroError","httpStatus":null,"severity":"error","filePath":"packages/astro/src/core/routing/rewrite.ts","lineNumber":153,"sourceCode":"\n/**\n * Utility function that creates a new `Request` with a new URL from an old `Request`.\n *\n * @param newUrl The new `URL`\n * @param oldRequest The old `Request`\n * @param isPrerendered It needs to be the flag of the previous routeData, before the rewrite\n * @param logger\n * @param routePattern\n */\nexport function copyRequest(\n\tnewUrl: URL,\n\toldRequest: Request,\n\tisPrerendered: boolean,\n\tlogger: AstroLogger,\n\troutePattern: string,\n): Request {\n\tif (oldRequest.bodyUsed) {\n\t\tthrow new AstroError(AstroErrorData.RewriteWithBodyUsed);\n\t}\n\treturn createRequest({\n\t\turl: newUrl,\n\t\tmethod: oldRequest.method,\n\t\tbody: oldRequest.body,\n\t\tisPrerendered,\n\t\tlogger,\n\t\theaders: isPrerendered ? {} : oldRequest.headers,\n\t\troutePattern,\n\t\tinit: {\n\t\t\treferrer: oldRequest.referrer,\n\t\t\treferrerPolicy: oldRequest.referrerPolicy,\n\t\t\tmode: oldRequest.mode,\n\t\t\tcredentials: oldRequest.credentials,\n\t\t\tcache: oldRequest.cache,\n\t\t\tredirect: oldRequest.redirect,\n\t\t\tintegrity: oldRequest.integrity,\n\t\t\tsignal: oldRequest.signal,","sourceCodeStart":135,"sourceCodeEnd":171,"githubUrl":"https://github.com/withastro/astro/blob/d081033d5fe8e8a68c4bbbad4af9d2deb9c74bca/packages/astro/src/core/routing/rewrite.ts#L135-L171","documentation":"RewriteWithBodyUsed: copyRequest() refused to build the rewrite request because oldRequest.bodyUsed is true. Astro.rewrite() needs to forward the original body to the new route; once the body has been consumed (e.g. await request.json()), it can no longer be transferred, so the rewrite is blocked.","triggerScenarios":"Awaiting Astro.request.json()/.formData()/.text() and then calling Astro.rewrite(); middleware reading the body before a downstream Astro.rewrite(); an action consuming input then rewriting.","commonSituations":"Reading POST data to decide whether to rewrite, then rewriting; middleware parsing the body; form handlers that inspect input before routing.","solutions":["Read from a clone: const data = await Astro.request.clone().formData(); then Astro.rewrite().","Reorder logic so Astro.rewrite() happens before any body read.","If the body is unneeded, rewrite without reading it at all."],"exampleFix":"// before\nconst data = await Astro.request.formData();\nAstro.rewrite('/other'); // bodyUsed === true → throws\n\n// after\nconst data = await Astro.request.clone().formData();\nAstro.rewrite('/other');","handlingStrategy":"validation","validationCode":"function bodyIsReadable(request: Request): boolean {\n  return !request.bodyUsed;\n}\n// before Astro.rewrite, ensure body hasn't been consumed","typeGuard":"function requestBodyAvailable(request: Request): boolean {\n  return !request.bodyUsed;\n}","tryCatchPattern":null,"preventionTips":["If you must read the body before rewriting, read from request.clone().","Reorder code so Astro.rewrite() precedes any body consumption.","In middleware, avoid reading the body unless you also clone it."],"tags":["rewrite","request-body","ssr","middleware"],"backgroundTag":null,"analyzedSha":"d081033d5fe8e8a68c4bbbad4af9d2deb9c74bca","analyzedAt":"2026-08-12T13:37:29.035Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}