{"record":{"id":"2635ddc69301f4ac","repo":"denoland/deno","slug":"err-http-body-not-allowed","errorCode":"ERR_HTTP_BODY_NOT_ALLOWED","errorMessage":"Adding content for this request method or response status is not allowed.","messagePattern":"Adding content for this request method or response status is not allowed\\.","errorType":"error_code","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"ext/node/polyfills/_http_outgoing.ts","lineNumber":1444,"sourceCode":"      );\n    }\n\n    msg[kBytesWritten] += len;\n  }\n\n  if (!msg._header) {\n    if (fromEnd) {\n      len ??= typeof chunk === \"string\"\n        ? Buffer.byteLength(chunk, encoding)\n        : TypedArrayPrototypeGetByteLength(chunk);\n      msg._contentLength = len;\n    }\n    msg._implicitHeader();\n  }\n\n  if (!msg._hasBody) {\n    if (msg[kRejectNonStandardBodyWrites]) {\n      throw new ERR_HTTP_BODY_NOT_ALLOWED();\n    }\n    debug(\n      \"This type of response MUST NOT have a body. \" +\n        \"Ignoring write() calls.\",\n    );\n    (globalThis as any).process.nextTick(callback);\n    return true;\n  }\n\n  // Auto-corking\n  if (!fromEnd && msg.socket && !msg.socket.writableCorked) {\n    msg.socket.cork();\n    (globalThis as any).process.nextTick(connectionCorkNT, msg.socket);\n  }\n\n  let ret;\n  if (msg.chunkedEncoding && chunk.length !== 0) {\n    len ??= typeof chunk === \"string\"","sourceCodeStart":1426,"sourceCodeEnd":1462,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/node/polyfills/_http_outgoing.ts#L1426-L1462","documentation":"write_() refuses to attach a body to a message with no body (msg._hasBody false - HEAD responses, 204/304) when the caller opted into strictness via the rejectNonStandardBodyWrites option (stored under kRejectNonStandardBodyWrites; default false). Instead of silently discarding the bytes, it throws ERR_HTTP_BODY_NOT_ALLOWED, matching Node's strict-mode semantics for non-standard bodies.","triggerScenarios":"new http.Server(server, { rejectNonStandardBodyWrites: true }) (or per-message option), then a handler calls res.write()/res.end(body) for a HEAD request or a 204/304 status, where the server marks _hasBody = false.","commonSituations":"Generic handlers/framework code that always writes a body, reused across HEAD requests and 204 'No Content' / 304 'Not Modified' responses; enabling the option on an existing app and discovering routes that write empty strings/bodies on no-content statuses; API gateways answering 204 after a write.","solutions":["Skip body writes for bodyless messages: if (req.method !== 'HEAD' && statusCode not in (204, 304)) write body.","Return a status that permits a body (e.g. 200 with empty body) when you must call end() uniformly.","If lenient behavior is acceptable, construct the server/message without rejectNonStandardBodyWrites (default false)."],"exampleFix":"// before\nres.writeHead(204);\nres.end('done'); // throws when rejectNonStandardBodyWrites\n\n// after\nres.writeHead(204);\nres.end(); // no chunk -> no body written","handlingStrategy":"validation","validationCode":"function canSendBody(req, statusCode) {\n  return req.method !== 'HEAD' && ![204, 304].includes(statusCode);\n}\nif (canSendBody(req, res.statusCode)) res.end(body); else res.end();","typeGuard":null,"tryCatchPattern":"try {\n  res.end(body);\n} catch (err) {\n  if (err.code === 'ERR_HTTP_BODY_NOT_ALLOWED') {\n    res.end(); // finish without a body\n  } else throw err;\n}","preventionTips":["Branch on method/status before writing: HEAD, 204 and 304 must never receive a body chunk.","Enable rejectNonStandardBodyWrites in tests to flush out bodyless-status writes early.","Answer with 200 + empty body when uniform 'always end(payload)' handler code is unavoidable."],"tags":["http","node-compat","http-semantics","stream"],"backgroundTag":"http-body-not-allowed","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"}