{"record":{"id":"9eb19c85196fef12","repo":"denoland/deno","slug":"the-body-of-the-response-returned-from-the-serve-h","errorCode":null,"errorMessage":"The body of the Response returned from the serve handler has already been consumed","messagePattern":"The body of the Response returned from the serve handler has already been consumed","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"ext/http/00_serve.ts","lineNumber":747,"sourceCode":"      // The Response prototype check above passes for Response-like objects\n      // (e.g. a subclass that skipped super(), or a Response from a different\n      // realm/polyfill). Those don't carry the internal slot we read from\n      // below, so reject them with a clear error instead of crashing later.\n      inner = getInnerResponse(response);\n      if (inner === undefined) {\n        throw new TypeError(\n          \"Return value from serve handler must be a Response constructed via the Response constructor in this realm\",\n        );\n      }\n\n      if (inner.type === \"error\") {\n        throw new TypeError(\n          \"Return value from serve handler must not be an error response (like Response.error())\",\n        );\n      }\n\n      if (responseBodyUsed(response)) {\n        throw new TypeError(\n          \"The body of the Response returned from the serve handler has already been consumed\",\n        );\n      }\n    } catch (error) {\n      try {\n        response = await onError(error);\n        if (!ObjectPrototypeIsPrototypeOf(ResponsePrototype, response)) {\n          throw new TypeError(\n            \"Return value from onError handler must be a response or a promise resolving to a response\",\n          );\n        }\n        inner = toInnerResponse(response);\n        if (inner === undefined) {\n          throw new TypeError(\n            \"Return value from onError handler must be a Response constructed via the Response constructor in this realm\",\n          );\n        }\n      } catch (error) {","sourceCodeStart":729,"sourceCodeEnd":765,"githubUrl":"https://github.com/denoland/deno/blob/89f33cbef296a2b287f323d42de54c871fa69c77/ext/http/00_serve.ts#L729-L765","documentation":"A Response body can be read exactly once. serve checks responseBodyUsed before sending, and throws this TypeError when the handler returns a Response whose body was already consumed by earlier code, because there is nothing left to put on the wire. The error is routed to onError (default: 500).","triggerScenarios":"Calling res.text()/res.json()/res.arrayBuffer() on the response before returning it; caching a single Response object and returning it for multiple requests; middleware that inspects a response body without cloning it.","commonSituations":"Logging response payloads (await res.text() then return res); singleton pre-built responses reused across requests; test code that verifies the body of the very object it returns.","solutions":["Read from a clone when you must inspect: await res.clone().text(), then return the original","Cache the source data and construct a fresh Response per request","In middleware, pass res.clone() downstream when you consume one copy","Check res.bodyUsed before returning and rebuild the Response if it is true"],"exampleFix":"// before\nDeno.serve(async () => {\n  const res = Response.json({ ok: true });\n  console.log(await res.text()); // consumes the body\n  return res;\n});\n\n// after\nDeno.serve(async () => {\n  const res = Response.json({ ok: true });\n  console.log(await res.clone().text()); // read the clone\n  return res;\n});","handlingStrategy":"validation","validationCode":"// Tripwire before returning from the handler\nif (response instanceof Response && response.bodyUsed) {\n  throw new Error(\"Response body was consumed before returning\");\n}\nreturn response;","typeGuard":"function isUnconsumedResponse(v) {\n  return v instanceof Response && !v.bodyUsed;\n}","tryCatchPattern":"try {\n  return await handler(req);\n} catch (err) {\n  if (err instanceof TypeError && err.message.includes(\"already been consumed\")) {\n    return new Response(\"Response body already consumed\", { status: 500 });\n  }\n  throw err;\n}","preventionTips":["Inspect bodies via clone() and return the unread original","Never cache Response instances across requests; cache the payload data","Check bodyUsed on responses that passed through middleware"],"tags":["http","serve","response","body","caching"],"backgroundTag":null,"analyzedSha":"89f33cbef296a2b287f323d42de54c871fa69c77","analyzedAt":"2026-08-16T07:54:21.310Z","schemaVersion":2},"datasetVersion":"2026-08-16T08:17:34.114Z"}