# unicorn/no-invalid-fetch-options

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

A `GET` or `HEAD` request can't have a body. If you give it one, `new Request()` throws a `TypeError` and `fetch()` rejects with one. The method defaults to `GET`, so the usual cause is a forgotten `method: "POST"`.

{/* Generated by src/codegen/lint-rule-docs.ts. Do not edit this section. */}

## Configure

Off by default. To turn it on:

```toml bunfig.toml
[lint.rules]
"unicorn/no-invalid-fetch-options" = "error" # or "warn"
```

{/* End of the generated section. */}

## Examples

Reported:

{/* bad */}
```js
function createUser(user) {
  return fetch("/api/users", {
    body: JSON.stringify(user),
  });
}
```

{/* bad */}
```js
const request = new Request("/api/users", {
  method: "HEAD",
  body: "name=Ada",
});
```

Not reported:

{/* good */}
```js
function createUser(user) {
  return fetch("/api/users", {
    method: "POST",
    body: JSON.stringify(user),
  });
}
```

A `body` of `undefined` or `null` is allowed. The rule also stays quiet when it can't tell what the method or the options are, for example when the object has a spread:

{/* good */}
```js
fetch("/api/users", { body: undefined });
fetch("/api/users", { ...defaults, body: "name=Ada" });
fetch("/api/users", { method: getMethod(), body: "name=Ada" });
```
