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.
Examples
Section titled “Examples”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" });const response = await fetch("/api");const response = await fetch("/api", { method: "POST", body: "data" });const response = await fetch("/api", { method: "PUT", body: "data" });const request = new Request("/api", { method: "POST", body: "data" });Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”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.
Further Reading
Section titled “Further Reading”Equivalents in Other Linters
Section titled “Equivalents in Other Linters”- ESLint:
unicorn/no-invalid-fetch-options - Oxlint:
unicorn/no-invalid-fetch-options
Made with ❤️🔥 in Boston by
Josh Goldberg and contributors.