Skip to content

fetchMethodBodies

Disallow providing a body with GET or HEAD fetch requests.

✅ This rule is included in the ts logical preset.

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 response = await fetch("/api", { body: "data" });
const response = await fetch("/api", { method: "GET", body: "data" });
const response = await fetch("/api", { method: "HEAD", body: "data" });
const request = new Request("/api", { body: "data" });
const request = new Request("/api", { method: "GET", 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 ❤️‍🔥 in Boston by Josh Goldberg and contributors.