Skip to content

noInvalidFetchOptions (JavaScript)

Language JavaScript (and super languages)
biome.json
{
"linter": {
"rules": {
"nursery": {
"noInvalidFetchOptions": "error"
}
}
}
}

Disallow a request body in fetch() and new Request() when the method is GET or HEAD.

fetch() and new Request() throw a TypeError when the options contain a body and the method is GET or HEAD. When the options don’t contain a method, the request uses GET.

The rule checks options written as an object directly in the call. It reports a method only when its value is known without running the code, for example a string, or a const variable that holds a string. A body set to undefined or null counts as no body. A spread, such as ...options, placed after body or method can replace them, so the rule doesn’t check those options.

await fetch("/", { body: "foo=bar" });
code-block.js:1:20 lint/nursery/noInvalidFetchOptions ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

✖ This request has a body, but its method is GET.

> 1 │ await fetch("/", { body: "foo=bar" });
│ ^^^^
2 │

ℹ Requests that use the GET or HEAD method can't have a body, so this code throws a TypeError when it runs. A request without a method uses GET.

ℹ Remove the body, or use a method that allows one, such as POST.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

new Request("/", { method: "GET", body: "foo=bar" });
code-block.js:1:35 lint/nursery/noInvalidFetchOptions ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

✖ This request has a body, but its method is GET.

> 1 │ new Request("/", { method: "GET", body: "foo=bar" });
│ ^^^^
2 │

ℹ Requests that use the GET or HEAD method can't have a body, so this code throws a TypeError when it runs. A request without a method uses GET.

ℹ Remove the body, or use a method that allows one, such as POST.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

await fetch("/", { method: "HEAD", body: "foo=bar" });
code-block.js:1:36 lint/nursery/noInvalidFetchOptions ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

✖ This request has a body, but its method is HEAD.

> 1 │ await fetch("/", { method: "HEAD", body: "foo=bar" });
│ ^^^^
2 │

ℹ Requests that use the GET or HEAD method can't have a body, so this code throws a TypeError when it runs. A request without a method uses GET.

ℹ Remove the body, or use a method that allows one, such as POST.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

await fetch("/", { method: "HEAD" });
await fetch("/", { method: "POST", body: "foo=bar" });
new Request("/", { method: "POST", body: "foo=bar" });
await fetch("/", { body: undefined });