{"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/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/fetch/23_request.js#L543-L579","documentation":"Request constructor step 35 enforces the fetch-spec rule that GET and HEAD requests have no body. The TypeError fires when init.body is non-null, or when the input is an existing Request that already carries a body and the effective method (from init.method or the input itself) is GET/HEAD.","triggerScenarios":"new Request(url, { method: \"GET\", body: \"x=1\" }); { method: \"HEAD\", body: formData }; new Request(existingPostRequest, { method: \"GET\" }) where the input request has a body.","commonSituations":"Toggling a POST helper to GET but keeping the body argument; generic request builders that always attach a body; wrapping an existing POST Request with a method override.","solutions":["Move the data into the URL query string for GET: url.searchParams.set(...)","Or switch the method to POST/PUT/PATCH if a body is required","When deriving from an input request, drop its body: pass init.body: null and don't reuse the input's body"],"exampleFix":"// before\nnew Request(\"https://example.com/search\", { method: \"GET\", body: \"q=deno\" }); // throws\n\n// after\nconst url = new URL(\"https://example.com/search\");\nurl.searchParams.set(\"q\", \"deno\");\nnew Request(url, { method: \"GET\" });","handlingStrategy":"validation","validationCode":"function buildRequest(url, method, body) {\n  const hasBody = body !== undefined && body !== null;\n  if ((method === \"GET\" || method === \"HEAD\") && hasBody) {\n    const u = new URL(url);\n    for (const [k, v] of new URLSearchParams(body)) u.searchParams.append(k, v);\n    return new Request(u, { method });\n  }\n  return new Request(url, { method, body });\n}","typeGuard":"function bodyAllowedForMethod(method: string, body: unknown): boolean {\n  const m = method.toUpperCase();\n  return !(m === \"GET\" || m === \"HEAD\") || body === undefined || body === null;\n}","tryCatchPattern":"try {\n  new Request(url, { method, body });\n} catch (err) {\n  if (err instanceof TypeError && err.message.includes(\"GET/HEAD\")) {\n    // move body into query params, or switch to POST — do not silently drop it\n    throw err;\n  } else throw err;\n}","preventionTips":["Encode GET payloads as query parameters (URL.searchParams), never as body","In generic request helpers, assert method-vs-body compatibility before constructing","When overriding an input request's method, also drop its body explicitly"],"tags":["fetch","request","http-method","body"],"backgroundTag":"get-request-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"}