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 step 6 enforces the fetch-spec null-body-status rule: statuses 204 (No Content), 205 (Reset Content), and 304 (Not Modified) must have no body. Supplying any body (string, ReadableStream, Blob, ...) together with one of these statuses throws TypeError "Response with null body status cannot have body". Pass null as the body for these statuses.
Solutions
- Return new Response(null, { status: 204 }) — the body must be null
- If you must send data, use 200 instead of 204/205/304
- In generic helpers, map body !== null && [204,205,304].includes(status) to a 200 or drop the body
Example fix
// before
new Response("{\"ok\":true}", { status: 204, headers: { "content-type": "application/json" } }); // throws
// after
new Response(null, { status: 204 }); // 204 must have no body
// or, if a payload is required:
new Response("{\"ok\":true}", { status: 200 }); Defensive patterns
Strategy: validation
Validate before calling
const NULL_BODY_STATUSES = new Set([204, 205, 304]);
function respond(body, init) {
if (NULL_BODY_STATUSES.has(init.status) && body !== null) {
return new Response(body, { ...init, status: 200 }); // or drop the body
}
return new Response(body, init);
} Type guard
const NULL_BODY = new Set([204, 205, 304]);
function bodyAllowedForStatus(status: number): boolean {
return !NULL_BODY.has(status);
} Try / catch
try {
return new Response(payload, { status });
} catch (err) {
if (err instanceof TypeError && err.message.includes("null body status")) {
return new Response(null, { status }); // success-with-no-body semantics kept
}
throw err;
} Prevention
- Make 204/205/304 handlers return new Response(null) by convention
- Guard generic JSON helpers: if body !== null, never use a null-body status
- When proxying 304s, forward the empty body exactly
When it happens
Trigger: new Response("no content", { status: 204 }); new Response(stream, { status: 304 }); forwarding a cached 304 upstream response while attaching a body.
Common situations: Handlers that always return a string body (return new Response(JSON.stringify(...), { status: 204 })) as a 'success, no data' signal; proxying 304s and accidentally re-attaching the cached body; using 204 for delete endpoints but also returning a message.
Related errors
- Body is unusable
- Invalid redirect status code
- The status provided ( ) is not equal to 101 and outside the…
- Body already consumed
- Body already consumed.
AI-assisted analysis of denoland/deno@9ad36f7a2c (2026-08-20).
Data as JSON: /api/errors/a1014c2470fd6ac7.
Report an issue: GitHub.
Appendix: 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 9ad36f7a2c)