From 47767e72f8ba04ca936d53d480c9dcedc2db9687 Mon Sep 17 00:00:00 2001 From: wbamberg Date: Thu, 20 Jun 2024 10:43:51 -0700 Subject: [PATCH] Explain a bit about headers and bodies --- files/en-us/web/api/fetch_api/using_fetch/index.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/files/en-us/web/api/fetch_api/using_fetch/index.md b/files/en-us/web/api/fetch_api/using_fetch/index.md index ce1681fab2679d5..c66ee9a8d2400ac 100644 --- a/files/en-us/web/api/fetch_api/using_fetch/index.md +++ b/files/en-us/web/api/fetch_api/using_fetch/index.md @@ -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. + To set a request body, pass it as the `body` option: ```js @@ -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: @@ -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