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

update fetch guide #34278

Merged
merged 14 commits into from
Jun 28, 2024
Prev Previous commit
Next Next commit
Explain a bit about headers and bodies
  • Loading branch information
wbamberg committed Jun 20, 2024
commit 47767e72f8ba04ca936d53d480c9dcedc2db9687
6 changes: 4 additions & 2 deletions files/en-us/web/api/fetch_api/using_fetch/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ If the `mode` option is set to `no-cors`, then `method` must be one of `GET`, `P

### Setting a body

The request body is the payload of the request: it's the thing the client is sending to the server. You won't include a body with `GET` requests, but you will include it in requests that send content to the server, such as {{httpmethod("POST")}} or {{httpmethod("PUT")}} requests. For example, if you want to upload a file to the server, you might make a `POST` request and include the file as the request body.
wbamberg marked this conversation as resolved.
Show resolved Hide resolved

To set a request body, pass it as the `body` option:

```js
Expand Down Expand Up @@ -123,6 +125,8 @@ See [Locked and disturbed streams](#locked_and_disturbed_streams) for more infor

### Setting headers

Request headers give the server information about the request: for example, the {{httpheader("Content-Type")}} header tells the server the format of the request's body. Many headers are set automatically by the browser and can't be set by a script: these are called {{glossary("Forbidden header name", "Forbidden header names")}}.

To set request headers, assign them to the `headers` option.

You can pass an object literal here containing `header-name: header-value` properties:
Expand All @@ -148,8 +152,6 @@ const response = await fetch("https://example.org/post", {
});
```

You can't set any header which has a {{glossary("Forbidden header name")}}.

If the `mode` option is set to `no-cors`, you can only set {{glossary("CORS-safelisted request header", "CORS-safelisted request headers")}}.

### Making POST requests
Expand Down