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

example of Fetch_API with body and URLSearchParams is invalid in both browser chrome and nodejs #38115

Open
NewUserHa opened this issue Feb 11, 2025 · 12 comments · May be fixed by #38118
Open
Labels
Content:WebAPI Web API docs good first issue A good issue for newcomers to get started with.

Comments

@NewUserHa
Copy link

MDN URL

https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch

What specific section or headline is this issue about?

No response

What information was incorrect, unhelpful, or incomplete?

copy from the documention:

const response = await fetch("https://example.org/post", {
  headers: {
    "Content-Type": "application/x-www-form-urlencoded",
  },
  // Automatically converted to "username=example&password=password"
  body: new URLSearchParams({ username: "example", password: "password" }),
  // ...
});

this code doesn't work in both browser chrome and nodejs, and both throw error "Uncaught TypeError TypeError: Failed to execute 'fetch' on 'Window': Request with GET/HEAD method cannot have body."

What did you expect to see?

it works

Do you have any supporting links, references, or citations?

No response

Do you have anything more you want to share?

No response

MDN metadata

Page report details
@NewUserHa NewUserHa added the needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened. label Feb 11, 2025
@github-actions github-actions bot added the Content:WebAPI Web API docs label Feb 11, 2025
@Josh-Cena Josh-Cena added good first issue A good issue for newcomers to get started with. and removed needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened. labels Feb 12, 2025
@Josh-Cena
Copy link
Member

I missed the method: "POST" option. Welcoming a PR to add it.

@NewUserHa
Copy link
Author

what about also adding a new example for method: "POST" ratherthan fetch(url+"?"+new URLSearchParams({ username: "example", password: "password" }))

@Josh-Cena
Copy link
Member

Oh actually, I reread this section, and what we are showing now is intended. This is not a complete code example that you can just copy and run; it only shows the parts relevant to our discussion. A complete POST request example is shown as late as https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#making_post_requests

@Josh-Cena Josh-Cena closed this as not planned Won't fix, can't repro, duplicate, stale Feb 12, 2025
@Josh-Cena Josh-Cena added closed: wontfix Out of scope, too much effort, or working as intended and removed good first issue A good issue for newcomers to get started with. labels Feb 12, 2025
@NewUserHa
Copy link
Author

but this code block is not marked as red(error) status like the one below it

@Josh-Cena
Copy link
Member

It's not intending to show "bad code". The // ... tells you that it's incomplete, with more content intending to come afterwards.

@NewUserHa
Copy link
Author

that // ... looks to be difficulty to understand, if it is incomplete code, then at least the code should be incomplete with invalid statement ... instead of valid // ...

@Josh-Cena Josh-Cena reopened this Feb 12, 2025
@github-actions github-actions bot added the needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened. label Feb 12, 2025
@Josh-Cena
Copy link
Member

I agree with you that it's hard to read. Shall we update all examples containing a body to use method: "POST"? There are many on this page.

@Josh-Cena Josh-Cena added good first issue A good issue for newcomers to get started with. and removed closed: wontfix Out of scope, too much effort, or working as intended needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened. labels Feb 12, 2025
@NewUserHa
Copy link
Author

the API references pages are good to read, these other pages are like tutories and usually too long to read for quick reference, it should be better to at least have good practice examples

@wbamberg
Copy link
Collaborator

This would take some careful rework IMO. The idea of this part of the guide is that it introduces three options, one at a time. It's not intentional that you can make a complete request at each stage. Then it shows how to combine them to make a complete POST request.

So if it's minimally changed to make a complete POST request at the point the body is introduced, then the next bit is incoherent.

@Josh-Cena
Copy link
Member

Shall we introduce the method first, then, since we talk about methods when we introduce the body option anyway?

@wbamberg
Copy link
Collaborator

wbamberg commented Feb 12, 2025

We do already introduce the method first. We could delete Making POST requests, and effectively cover it earlier, so:

  • when we introduce bodies we could say, for example, "note that if you include a body you must set method to POST (or whichever other methods)", and then include method:"POST" in that example
  • when we introduce headers we could expand a little on the existing "for example, the Content-Type header tells the server the format of the request's body.", to say some thing on the lines of "for example, in a POST request you can use the Content-Type header to tell the server the format of the request's body.", and then include body and method in that example.

I guess I'm a bit sensitive about this page because I spent some time turning it from a morass of random facts about fetch into something organized, and I don't want it to go down that route again.

@Josh-Cena
Copy link
Member

Yes, I think that would be a more reasonable order.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Content:WebAPI Web API docs good first issue A good issue for newcomers to get started with.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants