{"record":{"id":"a1014c2470fd6ac7","repo":"denoland/deno","slug":"response-with-null-body-status-cannot-have-body","errorCode":null,"errorMessage":"Response with null body status cannot have body","messagePattern":"Response with null body status cannot have body","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"ext/fetch/23_response.js","lineNumber":559,"sourceCode":"    if (\n      !tryFillSingleContentTypeHeader(\n        list,\n        init.headers,\n      )\n    ) {\n      fillHeaderList(\n        list,\n        init.headers,\n        prefix,\n        context,\n      );\n    }\n  }\n\n  // 6.\n  if (bodyWithType !== null) {\n    if (nullBodyStatus(response[_response].status)) {\n      throw new TypeError(\n        \"Response with null body status cannot have body\",\n      );\n    }\n\n    const { body, contentType } = bodyWithType;\n    response[_response].body = body;\n\n    if (contentType !== null) {\n      const list = responseHeaderList(response);\n      let hasContentType = false;\n      for (let i = 0; i < list.length; i++) {\n        if (byteLowerCase(list[i][0]) === \"content-type\") {\n          hasContentType = true;\n          break;\n        }\n      }\n      if (!hasContentType) {\n        ArrayPrototypePush(list, [\"Content-Type\", contentType]);","sourceCodeStart":541,"sourceCodeEnd":577,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/fetch/23_response.js#L541-L577","documentation":"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.","triggerScenarios":"new Response(\"no content\", { status: 204 }); new Response(stream, { status: 304 }); forwarding a cached 304 upstream response while attaching a body.","commonSituations":"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.","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"],"exampleFix":"// before\nnew Response(\"{\\\"ok\\\":true}\", { status: 204, headers: { \"content-type\": \"application/json\" } }); // throws\n\n// after\nnew Response(null, { status: 204 }); // 204 must have no body\n// or, if a payload is required:\nnew Response(\"{\\\"ok\\\":true}\", { status: 200 });","handlingStrategy":"validation","validationCode":"const NULL_BODY_STATUSES = new Set([204, 205, 304]);\nfunction respond(body, init) {\n  if (NULL_BODY_STATUSES.has(init.status) && body !== null) {\n    return new Response(body, { ...init, status: 200 }); // or drop the body\n  }\n  return new Response(body, init);\n}","typeGuard":"const NULL_BODY = new Set([204, 205, 304]);\nfunction bodyAllowedForStatus(status: number): boolean {\n  return !NULL_BODY.has(status);\n}","tryCatchPattern":"try {\n  return new Response(payload, { status });\n} catch (err) {\n  if (err instanceof TypeError && err.message.includes(\"null body status\")) {\n    return new Response(null, { status }); // success-with-no-body semantics kept\n  }\n  throw err;\n}","preventionTips":["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"],"tags":["fetch","response","status-code","body"],"backgroundTag":"null-body-status-with-body","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"}