denoland/deno · error · Error

ERR_HTTP_BODY_NOT_ALLOWED

ERR_HTTP_BODY_NOT_ALLOWED

Error message

Adding content for this request method or response status is not allowed.

What it means

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.

Solutions

  1. Skip body writes for bodyless messages: if (req.method !== 'HEAD' && statusCode not in (204, 304)) write body.
  2. Return a status that permits a body (e.g. 200 with empty body) when you must call end() uniformly.
  3. If lenient behavior is acceptable, construct the server/message without rejectNonStandardBodyWrites (default false).

Example fix

// before
res.writeHead(204);
res.end('done'); // throws when rejectNonStandardBodyWrites

// after
res.writeHead(204);
res.end(); // no chunk -> no body written
Defensive patterns

Strategy: validation

Validate before calling

function canSendBody(req, statusCode) {
  return req.method !== 'HEAD' && ![204, 304].includes(statusCode);
}
if (canSendBody(req, res.statusCode)) res.end(body); else res.end();

Try / catch

try {
  res.end(body);
} catch (err) {
  if (err.code === 'ERR_HTTP_BODY_NOT_ALLOWED') {
    res.end(); // finish without a body
  } else throw err;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of denoland/deno@9ad36f7a2c (2026-08-20). Data as JSON: /api/errors/2635ddc69301f4ac. Report an issue: GitHub.

Appendix: source

Thrown at ext/node/polyfills/_http_outgoing.ts:1444

      );
    }

    msg[kBytesWritten] += len;
  }

  if (!msg._header) {
    if (fromEnd) {
      len ??= typeof chunk === "string"
        ? Buffer.byteLength(chunk, encoding)
        : TypedArrayPrototypeGetByteLength(chunk);
      msg._contentLength = len;
    }
    msg._implicitHeader();
  }

  if (!msg._hasBody) {
    if (msg[kRejectNonStandardBodyWrites]) {
      throw new ERR_HTTP_BODY_NOT_ALLOWED();
    }
    debug(
      "This type of response MUST NOT have a body. " +
        "Ignoring write() calls.",
    );
    (globalThis as any).process.nextTick(callback);
    return true;
  }

  // Auto-corking
  if (!fromEnd && msg.socket && !msg.socket.writableCorked) {
    msg.socket.cork();
    (globalThis as any).process.nextTick(connectionCorkNT, msg.socket);
  }

  let ret;
  if (msg.chunkedEncoding && chunk.length !== 0) {
    len ??= typeof chunk === "string"

View on GitHub (pinned to 9ad36f7a2c)