Skip to content

fetchMethodBodies

Disallow providing a body with GET or HEAD fetch requests.

✅ This rule is included in the ts logical and logicalStrict presets.

The Fetch API throws a TypeError at runtime when the request method is GET or HEAD and a body is provided. This rule detects fetch() calls and new Request() calls that provide a body with a GET or HEAD method.

HTTP methods GET and HEAD are intended to retrieve data without sending a request body. Per the Fetch specification, providing a body with these methods is invalid and will cause the browser or runtime to throw an error.

const
const response: Response
response
= await
function fetch(input: string | URL | Request, init?: RequestInit): Promise<Response> (+1 overload)
fetch
("/api", {
RequestInit.body?: BodyInit | null

A BodyInit object or null to set request's body.

body
: "data" });
const
const response: Response
response
= await
function fetch(input: string | URL | Request, init?: RequestInit): Promise<Response> (+1 overload)
fetch
("/api", {
RequestInit.method?: string

A string to set request's method.

method
: "GET",
RequestInit.body?: BodyInit | null

A BodyInit object or null to set request's body.

body
: "data" });
const
const response: Response
response
= await
function fetch(input: string | URL | Request, init?: RequestInit): Promise<Response> (+1 overload)
fetch
("/api", {
RequestInit.method?: string

A string to set request's method.

method
: "HEAD",
RequestInit.body?: BodyInit | null

A BodyInit object or null to set request's body.

body
: "data" });
const
const request: Request
request
= new
var Request: new (input: RequestInfo | URL, init?: RequestInit) => Request

The Request interface of the Fetch API represents a resource request.

MDN Reference

Request
("/api", {
RequestInit.body?: BodyInit | null

A BodyInit object or null to set request's body.

body
: "data" });
const
const request: Request
request
= new
var Request: new (input: RequestInfo | URL, init?: RequestInit) => Request

The Request interface of the Fetch API represents a resource request.

MDN Reference

Request
("/api", {
RequestInit.method?: string

A string to set request's method.

method
: "GET",
RequestInit.body?: BodyInit | null

A BodyInit object or null to set request's body.

body
: "data" });

This rule is not configurable.

If your codebase uses a custom fetch wrapper or polyfill that accepts different options, this rule might produce false positives. You can disable it for files that use such wrappers.

Made with ❤️‍🔥 around the world by the Flint team and contributors.