{"record":{"id":"2d90a3036614a59b","repo":"denoland/deno","slug":"response-body-is-already-used","errorCode":null,"errorMessage":"Response body is already used","messagePattern":"Response body is already used","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"ext/cache/01_cache.js","lineNumber":176,"sourceCode":"    // Step 6.\n    if (innerResponse.status === 206) {\n      throw new TypeError(\"Response status must not be 206\");\n    }\n    // Step 7.\n    const varyHeader = getHeader(innerResponse.headerList, \"vary\");\n    if (varyHeader) {\n      const fieldValues = StringPrototypeSplit(varyHeader, \",\");\n      for (let i = 0; i < fieldValues.length; ++i) {\n        const field = fieldValues[i];\n        if (StringPrototypeTrim(field) === \"*\") {\n          throw new TypeError(\"Vary header must not contain '*'\");\n        }\n      }\n    }\n\n    // Step 8.\n    if (innerResponse.body !== null && innerResponse.body.unusable()) {\n      throw new TypeError(\"Response body is already used\");\n    }\n\n    const stream = innerResponse.body?.stream;\n    let rid = null;\n    if (stream) {\n      const resourceBacking = getReadableStreamResourceBacking(\n        innerResponse.body?.stream,\n      );\n      if (resourceBacking) {\n        rid = resourceBacking.rid;\n      } else {\n        rid = resourceForReadableStream(stream, innerResponse.body?.length);\n      }\n    }\n\n    // Remove fragment from request URL before put.\n    reqUrl.hash = \"\";\n","sourceCodeStart":158,"sourceCodeEnd":194,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/cache/01_cache.js#L158-L194","documentation":"Cache.put(request, response) requires the response body to be unread (spec step 8 checks innerResponse.body.unusable()). put() consumes the stream itself when storing, so a body that was already read, piped, or locked by a reader is rejected.","triggerScenarios":"`await res.text(); cache.put(req, res)`; calling res.json() before cache.put; obtaining a reader from res.body and then passing the response to cache.put.","commonSituations":"Logging or inspecting a response before caching it; routing the same Response object to both a render path and a cache path.","solutions":["Clone before reading: `await cache.put(req, res.clone())`, then consume the original","Or read from the clone and cache the untouched response","Check `res.bodyUsed === false` before calling cache.put"],"exampleFix":"// before\nconst text = await res.text();\nawait cache.put(req, res); // body already used -> throws\n\n// after\nawait cache.put(req, res.clone());\nconst text = await res.text();","handlingStrategy":"validation","validationCode":"if (response.bodyUsed) {\n  // body already consumed: clone earlier, or cache a fresh fetch\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Clone before reading: cache.put(req, res.clone())","Check response.bodyUsed === false before cache.put","Establish one owner for each Response body in your pipeline"],"tags":["cache-api","streams","response-body"],"backgroundTag":"response-body-already-used","analyzedSha":"9ad36f7a2cce60488e6ec52283efb32efddaf93a","analyzedAt":"2026-08-20T13:07:44.778Z","contentChangedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}