{"id":"2a3d465ab668b894","repo":"sindresorhus/got","slug":"the-options-method-method-cannot-be-used-with","errorCode":null,"errorMessage":"The `${options.method}` method cannot be used with a body","messagePattern":"The `(.+?)` method cannot be used with a body","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"source/core/index.ts","lineNumber":924,"sourceCode":"\t\t\t}\n\t\t}\n\n\t\treturn false;\n\t}\n\n\tprivate async _finalizeBody(): Promise<void> {\n\t\tconst {options} = this;\n\t\tconst headers = options.getInternalHeaders();\n\n\t\tconst isForm = !is.undefined(options.form);\n\t\t// eslint-disable-next-line @typescript-eslint/naming-convention\n\t\tconst isJSON = !is.undefined(options.json);\n\t\tconst isBody = !is.undefined(options.body);\n\t\tconst cannotHaveBody = !this._methodCanHaveBody;\n\n\t\tif (isForm || isJSON || isBody) {\n\t\t\tif (cannotHaveBody) {\n\t\t\t\tthrow new TypeError(`The \\`${options.method}\\` method cannot be used with a body`);\n\t\t\t}\n\n\t\t\t// Serialize body\n\t\t\tconst noContentType = !is.string(headers['content-type']);\n\n\t\t\tif (isBody) {\n\t\t\t\t// Native FormData\n\t\t\t\tif (options.body instanceof FormData) {\n\t\t\t\t\tconst {body, contentType} = serializeNativeFormDataBody(options.body);\n\t\t\t\t\tthis._nativeFormDataBody = {\n\t\t\t\t\t\tform: options.body,\n\t\t\t\t\t\tbody,\n\t\t\t\t\t\tcontentTypeWasGenerated: noContentType,\n\t\t\t\t\t};\n\n\t\t\t\t\tif (noContentType) {\n\t\t\t\t\t\theaders['content-type'] = contentType;\n\t\t\t\t\t}","sourceCodeStart":906,"sourceCodeEnd":942,"githubUrl":"https://github.com/sindresorhus/got/blob/e3924aa1e53a6ca3eb93a43618ce532442a89b40/source/core/index.ts#L906-L942","documentation":"Thrown at source/core/index.ts:924 inside `_finalizeBody`. got forbids a request body on methods that semantically cannot have one: GET and HEAD by default (the `methodsWithoutBody` set at core/index.ts:86), unless `allowGetBody: true` is set for GET. If you supply `body`, `json`, or `form` with one of those methods, got rejects the call rather than letting Node silently drop the body or send an invalid request. This is a deliberate RFC alignment (RFC 9110 §9.3.1 discourages payloads on GET and forbids them on HEAD).","triggerScenarios":"Passing `json`, `form`, or `body` together with `method: 'GET'` or `method: 'HEAD'`; setting a global default body in got.extend() and then issuing a GET; converting a POST call to GET but leaving the body option in place.","commonSituations":"Refactoring a request from POST to GET and forgetting to drop the body; setting a default body in extend() that applies to all methods; mixing a searchParams-style payload with a body-based method; GraphQL clients that always send a body but switch to GET for GET-style queries.","solutions":["Move the payload into `searchParams` for GET/HEAD requests, where query strings belong.","Change the method to POST/PUT/PATCH if you genuinely need to send a body.","If you must send a body on GET (non-standard), set `allowGetBody: true` — note this still forbids a body on HEAD."],"exampleFix":"// before\nawait got('https://api/search', { method: 'GET', json: { query: 'foo' } });\n\n// after — send the payload as query string\nawait got('https://api/search', { method: 'GET', searchParams: { query: 'foo' } });","handlingStrategy":"validation","validationCode":"const METHODS_WITHOUT_BODY = new Set(['GET', 'HEAD']);\n\nfunction assertBodyAllowedForMethod(method, hasBody, allowGetBody = false) {\n  const forbidden = method === 'HEAD' || (method === 'GET' && !allowGetBody);\n  if (forbidden && hasBody) {\n    throw new TypeError(`The \\`${method}\\` method cannot be used with a body — use searchParams or change the method.`);\n  }\n}\n\nconst opts = { method: 'GET', json: payload };\nassertBodyAllowedForMethod(opts.method, Boolean(opts.json || opts.body || opts.form));","typeGuard":"function methodCanHaveBody(method: string, allowGetBody = false): boolean {\n  if (method === 'HEAD') return false;\n  if (method === 'GET') return allowGetBody;\n  return true;\n}","tryCatchPattern":"try {\n  await got(url, { method, json: payload });\n} catch (error) {\n  if (error instanceof TypeError && /method cannot be used with a body/.test(error.message)) {\n    // move the payload to query string or switch method\n    if (method === 'GET' || method === 'HEAD') {\n      return got(url, { method, searchParams: payload });\n    }\n  }\n  throw error;\n}","preventionTips":["Use searchParams for GET/HEAD payloads; reserve body/json/form for POST/PUT/PATCH/DELETE.","If you set a default body/json in got.extend, scope that extend to write methods only.","Set allowGetBody: true only if the upstream explicitly accepts a GET body (non-standard)."],"tags":["http-method","body","rfc","validation"],"analyzedSha":"e3924aa1e53a6ca3eb93a43618ce532442a89b40","analyzedAt":"2026-08-03T19:22:24.770Z","schemaVersion":2}