{"record":{"id":"85431c8502326d71","repo":"denoland/deno","slug":"request-with-get-head-method-cannot-have-body","errorCode":null,"errorMessage":"Request with GET/HEAD method cannot have body","messagePattern":"Request with GET/HEAD method cannot have body","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"ext/fetch/23_request.js","lineNumber":561,"sourceCode":"      if (headerList.length !== 0) {\n        ArrayPrototypeSplice(headerList, 0, headerList.length);\n      }\n      fillHeaders(this[_headers], headers);\n    }\n\n    // 34.\n    let inputBody = null;\n    if (ObjectPrototypeIsPrototypeOf(RequestPrototype, input)) {\n      inputBody = input[_body];\n    }\n\n    // 35.\n    if (\n      (request.method === \"GET\" || request.method === \"HEAD\") &&\n      ((init.body !== undefined && init.body !== null) ||\n        inputBody !== null)\n    ) {\n      throw new TypeError(\"Request with GET/HEAD method cannot have body\");\n    }\n\n    // 36.\n    let initBody = null;\n\n    // 37.\n    if (init.body !== undefined && init.body !== null) {\n      const res = extractBody(init.body);\n      initBody = res.body;\n      if (res.contentType !== null && !this[_headers].has(\"content-type\")) {\n        this[_headers].append(\"Content-Type\", res.contentType);\n      }\n    }\n\n    // 38.\n    const inputOrInitBody = initBody ?? inputBody;\n\n    // 40.","sourceCodeStart":543,"sourceCodeEnd":579,"githubUrl":"https://github.com/denoland/deno/blob/89f33cbef296a2b287f323d42de54c871fa69c77/ext/fetch/23_request.js#L543-L579","documentation":"Request constructor step 35 throws TypeError when the resolved method is GET or HEAD and a body is supplied — either via init.body (any non-null/undefined value, including '' or new Blob()) or when the input is a Request that already carries a body. GET/HEAD have no request body semantics in fetch.","triggerScenarios":"new Request(url, { method: 'GET', body: 'x' }), fetch(url, { method: 'HEAD', body: formData }), or new Request(inputRequestWithBody, { method: 'GET' }) where the input body is retained.","commonSituations":"Generic wrappers that always set body, switching a POST call to GET during debugging while leaving the body argument, or converting query-parameter APIs to body-carrying GETs.","solutions":["Remove the body for GET/HEAD requests; put the data in the URL query string instead","If a body is required, use POST/PUT/PATCH","In shared helpers, set body only when it is non-empty and the method allows one"],"exampleFix":"// before\nawait fetch(`https://api.example/items`, {\n  method: 'GET',\n  body: JSON.stringify({ page: 2 }),\n});\n\n// after\nawait fetch(`https://api.example/items?page=2`, {\n  method: 'GET',\n});","handlingStrategy":"type-guard","validationCode":"const BODYLESS = new Set(['GET', 'HEAD']);\nfunction buildInit(method, data) {\n  const m = String(method).toUpperCase();\n  return BODYLESS.has(m) ? { method: m } : { method: m, body: data };\n}","typeGuard":"const methodAllowsBody = (m) => !['GET', 'HEAD'].includes(String(m).toUpperCase());","tryCatchPattern":null,"preventionTips":["Encode GET payloads as query parameters, never as body","In shared fetch helpers, set body conditionally on the method","Remember an input Request carrying a body also triggers this when you override method to GET/HEAD"],"tags":["fetch","request","http-method","body"],"backgroundTag":null,"analyzedSha":"89f33cbef296a2b287f323d42de54c871fa69c77","analyzedAt":"2026-08-16T07:54:21.310Z","schemaVersion":2},"datasetVersion":"2026-08-16T08:17:34.114Z"}