Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rule proposal: force method: "POST" on fetch calls with body #1989

Closed
GauBen opened this issue Nov 22, 2022 · 7 comments · Fixed by #2338
Closed

Rule proposal: force method: "POST" on fetch calls with body #1989

GauBen opened this issue Nov 22, 2022 · 7 comments · Fixed by #2338

Comments

@GauBen
Copy link

GauBen commented Nov 22, 2022

Description

I sometimes forget to add method: 'POST' to fetch calls

image

Fail

fetch('http://example.com', { body: '{}' })

Pass

fetch('http://example.com', { method: 'POST', body: '{}' })

Additional Info

No response

@dimaMachina
Copy link
Contributor

dimaMachina commented Nov 22, 2022

Another thing related to fetch - disallow specifying method: 'GET' as it will be set by default

@fisker
Copy link
Collaborator

fisker commented Nov 22, 2022

Should not enforce POST, but forbid 'GET'(or omitted)/'HEAD'

@silverwind
Copy link
Contributor

silverwind commented Nov 22, 2022

No HTTP method strictly forbids a body, and there are definitely examples of APIs that accept a body on GET, like elasticsearch.

@fisker
Copy link
Collaborator

fisker commented Nov 30, 2022

No HTTP method strictly forbids a body

The fetch spec forbid it,

If either init["body"] exists and is non-null or inputBody is non-null, and request’s method is GET or HEAD, then throw a TypeError.

https://fetch.spec.whatwg.org/#request-class

@silverwind
Copy link
Contributor

silverwind commented Nov 30, 2022

Interesting, didn't know that. I guess some implemententations may opt to not implement that step, but for a general case, it seems good to have the rule for it then.

I do forsee that the spec may change in the future, similar to how initially a body on DELETE was forbidden by older RFCs, but newer specs now say it MAY have a body 😉. At least OpenAPI reverted their previous policy and they do allow body on GET,HEAD and DELETE.

@fisker
Copy link
Collaborator

fisker commented May 8, 2024

no-invalid-fetch-options?

@sindresorhus
Copy link
Owner

no-invalid-fetch-options

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants