denoland/deno · error · TypeError

Response with null body status cannot have body

Error message

Response with null body status cannot have body

What it means

initializeAResponse rejects a non-null body when the status is a null-body status (204 No Content, 205 Reset Content, 304 Not Modified) with TypeError 'Response with null body status cannot have body'. These statuses are defined to carry no payload, so attaching one is a spec violation caught at construction.

Source

Thrown at ext/fetch/23_response.js:559

    if (
      !tryFillSingleContentTypeHeader(
        list,
        init.headers,
      )
    ) {
      fillHeaderList(
        list,
        init.headers,
        prefix,
        context,
      );
    }
  }

  // 6.
  if (bodyWithType !== null) {
    if (nullBodyStatus(response[_response].status)) {
      throw new TypeError(
        "Response with null body status cannot have body",
      );
    }

    const { body, contentType } = bodyWithType;
    response[_response].body = body;

    if (contentType !== null) {
      const list = responseHeaderList(response);
      let hasContentType = false;
      for (let i = 0; i < list.length; i++) {
        if (byteLowerCase(list[i][0]) === "content-type") {
          hasContentType = true;
          break;
        }
      }
      if (!hasContentType) {
        ArrayPrototypePush(list, ["Content-Type", contentType]);

View on GitHub (pinned to 89f33cbef2)

Solutions

  1. Return new Response(null, { status: 204 }) — omit the body entirely
  2. If you must send data, use 200 with the body instead of 204
  3. In response-building helpers, force body = null whenever status is 204/205/304

Example fix

// before
return new Response(JSON.stringify({ ok: true }), {
  status: 204,
  headers: { 'content-type': 'application/json' },
});

// after
return new Response(null, { status: 204 });
Defensive patterns

Strategy: type-guard

Validate before calling

const NULL_BODY = new Set([204, 205, 304]);
function buildResponse(body, init) {
  const status = init.status ?? 200;
  return new Response(NULL_BODY.has(status) ? null : body, init);
}

Type guard

const isNullBodyStatus = (s) => [204, 205, 304].includes(s);

Prevention

When it happens

Trigger: new Response('deleted', { status: 204 }), new Response(jsonString, { status: 304 }), Response.json(data, { status: 205 }).

Common situations: REST handlers returning 204 with a success message body, cache revalidation helpers copying a body onto 304, or status-config tables that pair 204 with default body templates.

Related errors


AI-assisted analysis of denoland/deno@89f33cbef2 (2026-08-16). Data as JSON: /api/errors/a1014c2470fd6ac7. Report an issue: GitHub.