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>
fetch
("/api", {
RequestInit.body?: BodyInit
body
: "data" });
const
const response: Response
response
= await
function fetch(input: string | URL | Request, init?: RequestInit): Promise<Response>
fetch
("/api", {
RequestInit.method?: string
method
: "GET",
RequestInit.body?: BodyInit
body
: "data" });
const
const response: Response
response
= await
function fetch(input: string | URL | Request, init?: RequestInit): Promise<Response>
fetch
("/api", {
RequestInit.method?: string
method
: "HEAD",
RequestInit.body?: BodyInit
body
: "data" });
const
const request: Request
request
= new
var Request: new (input: RequestInfo, init?: RequestInit) => Request
Request
("/api", {
RequestInit.body?: BodyInit
body
: "data" });
const
const request: Request
request
= new
var Request: new (input: RequestInfo, init?: RequestInit) => Request
Request
("/api", {
RequestInit.method?: string
method
: "GET",
RequestInit.body?: BodyInit
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.