Skip to content

Commit 79931e9

Browse files
committed
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 f844d5c commit 79931e9

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
@@ -8385,6 +8385,56 @@ correctly. This section aims to give some advice.
83858385

83868386
<p class=XXX>This is a work in progress.
83878387

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

83898439
<h3 id=fetch-elsewhere-fetch class=no-num>Invoking fetch</h3>
83908440

0 commit comments

Comments
 (0)