oxc-project/oxc · error · OxcDiagnostic

"body" is not allowed when method is "{method}"

Error message

"body" is not allowed when method is "{method}"

What it means

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.

Source

Thrown at crates/oxc_linter/src/rules/unicorn/no_invalid_fetch_options.rs:22

use cow_utils::CowUtils;
use oxc_allocator::ArenaBox;
use oxc_ast::{
    AstKind,
    ast::{
        Argument, Expression, FormalParameter, ObjectExpression, ObjectPropertyKind, PropertyKey,
        TSLiteral, TSLiteralType, TSType, TSTypeAnnotation, TemplateLiteral,
    },
};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_semantic::AstNode;
use oxc_span::Span;
use oxc_str::CompactStr;

fn no_invalid_fetch_options_diagnostic(span: Span, method: &str) -> OxcDiagnostic {
    let message = format!(r#""body" is not allowed when method is "{method}""#);

    OxcDiagnostic::warn(message).with_label(span)
}

#[derive(Debug, Default, Clone)]
pub struct NoInvalidFetchOptions;

declare_oxc_lint!(
    /// ### What it does
    ///
    /// Disallow invalid options in `fetch()` and `new Request()`. Specifically, this rule ensures that
    /// a body is not provided when the method is `GET` or `HEAD`, as it will result in a `TypeError`.
    ///
    /// ### Why is this bad?
    ///
    /// The `fetch()` function throws a `TypeError` when the method is `GET` or `HEAD` and a body is provided.
    /// This can lead to unexpected behavior and errors in your code. By disallowing such invalid options,
    /// the rule ensures that requests are correctly configured and prevents unnecessary errors.
    ///
    /// ### Examples

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Make the body conditional on the method: `{ method, ...(hasBody ? { body } : {}) }`
  2. Drop the body for GET/HEAD calls, or change the method to POST/PUT/PATCH when a body is required
  3. In shared clients, throw early when both are configured so misuse fails at the wrapper boundary, not inside fetch

Example fix

// before
await fetch(url, { method: 'GET', body: formData });

// after
await fetch(url, { method: 'GET' });
// send formData with method: 'POST' instead
Defensive patterns

Strategy: validation

Validate before calling

function buildInit(method: string, body?: BodyInit): RequestInit {
  const m = method.toUpperCase();
  if ((m === 'GET' || m === 'HEAD') && body !== undefined) {
    throw new TypeError(`body is not allowed with ${m} requests`);
  }
  return body === undefined ? { method } : { method, body };
}

Try / catch

try {
  await fetch(url, init);
} catch (err) {
  if (err instanceof TypeError) {
    // invalid options (e.g. body with GET/HEAD) — fix the call site
  }
  throw err;
}

Prevention

When it happens

Trigger: `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.

Common situations: 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.

Related errors


AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20). Data as JSON: /api/errors/af1644b19d6e2340. Report an issue: GitHub.