This rule raises an issue when fetch() or new Request() is called with a body option while using GET or HEAD methods.

Why is this an issue?

The HTTP specification defines that GET and HEAD requests should not contain a request body. When you provide a body with these methods, the Fetch API throws a TypeError at runtime.

GET requests are designed to retrieve data from a server without modifying anything. Including a body contradicts this purpose and violates HTTP standards. Similarly, HEAD requests are meant to retrieve only headers, making a body meaningless.

When no method is specified in fetch options, GET is used by default. This means that providing a body without explicitly setting a method like POST will also cause the same error.

This issue typically occurs when developers copy request code between different HTTP methods without adjusting the options appropriately.

What is the potential impact?

This code will throw a TypeError at runtime, causing the application to crash or behave unexpectedly. Users may experience broken functionality, and the error might not be caught during development if the problematic code path isn’t tested thoroughly.

How to fix?

Remove the body option when using GET requests (default method).

Non-compliant code example

const response = await fetch('/', {body: 'foo=bar'}); // Noncompliant

Compliant code example

const response = await fetch('/');

Documentation

Standards