{"record":{"id":"af1644b19d6e2340","repo":"oxc-project/oxc","slug":"body-is-not-allowed-when-method-is-method","errorCode":null,"errorMessage":"\"body\" is not allowed when method is \"{method}\"","messagePattern":"\"body\" is not allowed when method is \"(.+?)\"","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"error","filePath":"crates/oxc_linter/src/rules/unicorn/no_invalid_fetch_options.rs","lineNumber":22,"sourceCode":"use cow_utils::CowUtils;\nuse oxc_allocator::ArenaBox;\nuse oxc_ast::{\n    AstKind,\n    ast::{\n        Argument, Expression, FormalParameter, ObjectExpression, ObjectPropertyKind, PropertyKey,\n        TSLiteral, TSLiteralType, TSType, TSTypeAnnotation, TemplateLiteral,\n    },\n};\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_semantic::AstNode;\nuse oxc_span::Span;\nuse oxc_str::CompactStr;\n\nfn no_invalid_fetch_options_diagnostic(span: Span, method: &str) -> OxcDiagnostic {\n    let message = format!(r#\"\"body\" is not allowed when method is \"{method}\"\"#);\n\n    OxcDiagnostic::warn(message).with_label(span)\n}\n\n#[derive(Debug, Default, Clone)]\npub struct NoInvalidFetchOptions;\n\ndeclare_oxc_lint!(\n    /// ### What it does\n    ///\n    /// Disallow invalid options in `fetch()` and `new Request()`. Specifically, this rule ensures that\n    /// a body is not provided when the method is `GET` or `HEAD`, as it will result in a `TypeError`.\n    ///\n    /// ### Why is this bad?\n    ///\n    /// The `fetch()` function throws a `TypeError` when the method is `GET` or `HEAD` and a body is provided.\n    /// This can lead to unexpected behavior and errors in your code. By disallowing such invalid options,\n    /// the rule ensures that requests are correctly configured and prevents unnecessary errors.\n    ///\n    /// ### Examples","sourceCodeStart":4,"sourceCodeEnd":40,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/unicorn/no_invalid_fetch_options.rs#L4-L40","documentation":"Diagnostic from the oxlint rule `unicorn/no-invalid-fetch-options`. `fetch()` and `new Request()` throw `TypeError: Request with GET/HEAD method cannot have body` when a body is supplied together with method GET or HEAD. The rule reports statically whenever an options object has both `body` and a method that resolves to GET/HEAD — including string literals, template literals, and TypeScript literal-union method types — turning a guaranteed runtime crash into a lint-time finding.","triggerScenarios":"`fetch(url, { method: 'GET', body: data })`, `new Request(url, { method: 'HEAD', body: JSON.stringify(x) })`, or a method typed `'GET' | 'POST'` in TypeScript with an unconditional `body` property in the options object.","commonSituations":"Generic request wrappers that always attach a body; switching a call from POST to GET/HEAD while leaving the body behind; HEAD health checks reusing POST configs; typed API clients with method unions.","solutions":["Make the body conditional on the method: `{ method, ...(hasBody ? { body } : {}) }`","Drop the body for GET/HEAD calls, or change the method to POST/PUT/PATCH when a body is required","In shared clients, throw early when both are configured so misuse fails at the wrapper boundary, not inside fetch"],"exampleFix":"// before\nawait fetch(url, { method: 'GET', body: formData });\n\n// after\nawait fetch(url, { method: 'GET' });\n// send formData with method: 'POST' instead","handlingStrategy":"validation","validationCode":"function buildInit(method: string, body?: BodyInit): RequestInit {\n  const m = method.toUpperCase();\n  if ((m === 'GET' || m === 'HEAD') && body !== undefined) {\n    throw new TypeError(`body is not allowed with ${m} requests`);\n  }\n  return body === undefined ? { method } : { method, body };\n}","typeGuard":null,"tryCatchPattern":"try {\n  await fetch(url, init);\n} catch (err) {\n  if (err instanceof TypeError) {\n    // invalid options (e.g. body with GET/HEAD) — fix the call site\n  }\n  throw err;\n}","preventionTips":["Never set body on GET/HEAD; centralize init-building in one validated helper","Type method as a string union and let the helper reject GET/HEAD plus body","Treat TypeError from fetch as a programming error, not a network failure"],"tags":["fetch","http","runtime-error","browser","typescript","oxlint"],"backgroundTag":"invalid-fetch-options","analyzedSha":"e1e7af627c8843ab64044ed466b128fcc21a035b","analyzedAt":"2026-08-20T07:01:07.079Z","contentChangedAt":"2026-08-20T07:01:07.079Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}