Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2e63509

Browse files
committedJan 2, 2023
Editorial: Add prose about constructing a request
This intends to be a summary of informative notes in the #Requests section, into a concise "starter" guide to populating a request, which is usually the tricky bit of using Fetch. Requested by authors of other specs, see for example: w3ctag/design-principles#238 w3c-fedid/FedCM#320
1 parent 464326e commit 2e63509

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed
 

‎fetch.bs

+50
Original file line numberDiff line numberDiff line change
@@ -8380,6 +8380,56 @@ correctly. This section aims to give some advice.
83808380

83818381
<p class=XXX>This is a work in progress.
83828382

8383+
<h3 id=fetch-elsewhere-request>Constructing a request</h3>
8384+
8385+
<p>The first step in <a for=/>fetching</a> is to create a <a for=/>request</a>, and populate its
8386+
multiple associated parameters.
8387+
8388+
<p>A <a for=/>request</a> oftenmost has a <a for=request>client</a>, which is the
8389+
<a>environment settings object</a> that initiatated the request, and the receiver of the different
8390+
<a for=/>response</a> callbacks. It then expects a <a for=request>URL</a>, and an HTTP
8391+
<a for=request>method</a>, `<code>GET</code>` by default. `<code>POST</code>` and
8392+
`<code>PUT</code>` requests also accept a <a for=request>body</a>, which is sent together with the
8393+
request.
8394+
8395+
<p>Requests accept an optional <a for=request>header list</a> parameter, custom HTTP headers to be
8396+
sent alongside the <a for=/>request</a>. Note that sending custom headers may have implications,
8397+
such as requiring a <a>CORS-preflight fetch</a>, so handle with care.
8398+
8399+
<p>One of the most important things when constructing a <a for=/>request</a> is understanding how
8400+
it's supposed to handle cross-origin resources. This is expressed in the <a for=/>request</a>'s
8401+
<a for=request>mode</a>. Some features are supposed to work only in the context of the
8402+
<a>same origin</a>, in which case the "<code>same-origin</code>" <a for=request>mode</a> is
8403+
appropriate. For new features that require accessing cross-origin resources, "<code>cors</code>"
8404+
should be used, alongside a <a for=request>credentials mode</a>, which defines whether the
8405+
<a for=/>request</a> is anonymous or authenticated (e.g. with cookies and client certificates). The
8406+
"<code>no-cors</code>" <a for=request>mode</a> is used for backwards compatibility with features
8407+
that existed before the CORS mechanism was introduced. To use "<code>no-cors</code>" in a new
8408+
feature, please discuss on the <a href=https://github.com/whatwg/fetch>Fetch Github repository</a>.
8409+
"<code>navigate</code>" and "<code>websocket</code>" are reserved for specific features.
8410+
8411+
<p>A <a for=/>request</a> accepts a few security-related parameters that are often configured, such
8412+
as <a for=request>integrity metadata</a>, a string hash of the actual data that verifies that the
8413+
received resource matches an expectation, a <a for=request>cryptographic nonce metadata</a> and
8414+
<a for=request>parser metadata</a>, which allows more fine-grained security via
8415+
<cite>Content Security Policy</cite>, and <a for=request>referrer policy</a>, which allows the
8416+
<a for=/>request</a> to use a different <a for=/>referrer policy</a> from the one provided by the
8417+
<a for=request>client</a>. [[!CSP]] [[!REFERRER]]
8418+
8419+
<p>A <a for=/>request</a> also accepts a few metadata parameters, the important one being
8420+
<a for=request>destination</a>. <a for=request>destinations</a> affect
8421+
<cite>Content Security Policy</cite> and have other implications such as the
8422+
`<code>sec-fetch-dest</code>` header, so they are much more than informative metadata. If a new
8423+
feature requires a <a for=request>destination</a> that's not in the list of
8424+
<a for=request>destinations</a>, please file an issue in the
8425+
<a href=https://github.com/whatwg/fetch>Fetch Github repository</a>. An aditional metadata
8426+
paramater is <a for=request>initiator type</a>, which specifies that the <a for=/>fetching</a>
8427+
should <a for=/>mark resource timing</a> automatically once the <a for=/>response</a>'s
8428+
<a for=response>body</a> is fully downloaded. [[!CSP]] [[RESOURCE-TIMING]]
8429+
8430+
<p>The rest of the parameters for <a for=/>request</a> are used less frequently, and they are
8431+
documented in detail in the <a href="#requests">Requests</a> section of this standard.
8432+
83838433

83848434
<h3 id=fetch-elsewhere-fetch class=no-num>Invoking fetch</h3>
83858435

0 commit comments

Comments
 (0)
Please sign in to comment.