{"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/89f33cbef296a2b287f323d42de54c871fa69c77/ext/fetch/23_response.js#L541-L577","documentation":"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.","triggerScenarios":"new Response('deleted', { status: 204 }), new Response(jsonString, { status: 304 }), Response.json(data, { status: 205 }).","commonSituations":"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.","solutions":["Return new Response(null, { status: 204 }) — omit the body entirely","If you must send data, use 200 with the body instead of 204","In response-building helpers, force body = null whenever status is 204/205/304"],"exampleFix":"// before\nreturn new Response(JSON.stringify({ ok: true }), {\n  status: 204,\n  headers: { 'content-type': 'application/json' },\n});\n\n// after\nreturn new Response(null, { status: 204 });","handlingStrategy":"type-guard","validationCode":"const NULL_BODY = new Set([204, 205, 304]);\nfunction buildResponse(body, init) {\n  const status = init.status ?? 200;\n  return new Response(NULL_BODY.has(status) ? null : body, init);\n}","typeGuard":"const isNullBodyStatus = (s) => [204, 205, 304].includes(s);","tryCatchPattern":null,"preventionTips":["Always pass null as the body for 204/205/304","Return 200 with a body when a success payload is required","Centralize response construction in one helper that enforces the rule"],"tags":["fetch","response","status-code","body"],"backgroundTag":null,"analyzedSha":"89f33cbef296a2b287f323d42de54c871fa69c77","analyzedAt":"2026-08-16T07:54:21.310Z","schemaVersion":2},"datasetVersion":"2026-08-16T08:17:34.114Z"}