+ For more than a decade the Web has used XMLHttpRequest (XHR) to achieve
asynchronous requests in JavaScript. While very useful, XHR is not a very
nice API. It suffers from lack of separation of concerns. The input, output
and state are all managed by interacting with one object, and state is
@@ -13,10 +15,12 @@
The Fetch specification, which
defines the API, nails down the semantics of a user agent fetching a resource.
This, combined with ServiceWorkers, is an attempt to:
- - Improve the offline experience.
+
+ - Improve the offline experience.
- Expose the building blocks of the Web to the platform as part of the
extensible web movement.
-
As of this writing, the Fetch API is available in Firefox 39 (currently
+
+ As of this writing, the Fetch API is available in Firefox 39 (currently
Nightly) and Chrome 42 (currently dev). Github has a Fetch polyfill.
Feature detection
@@ -29,8 +33,7 @@
The most useful, high-level part of the Fetch API is the fetch()
function.
In its simplest form it takes a URL and returns a promise that resolves
to the response. The response is captured as a Response
object.
-
-
fetch("/data.json").then(function(res) {
+ fetch("/data.json").then(function(res) {
// res instanceof Response == true.
if (res.ok) {
res.json().then(function(data) {
@@ -42,10 +45,9 @@
}, function(e) {
console.log("Fetch failed!", e);
});
-
+
Submitting some parameters, it would look like this:
-
-
fetch("http://www.example.org/submit.php", {
+ fetch("http://www.example.org/submit.php", {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded"
@@ -60,84 +62,84 @@
}, function(e) {
alert("Error submitting form!");
});
-
+
The fetch()
function’s arguments are the same as those passed
to the
-
Request()
constructor, so you may directly pass arbitrarily
+
+Request()
constructor, so you may directly pass arbitrarily
complex requests to fetch()
as discussed below.
Headers
Fetch introduces 3 interfaces. These are Headers
, Request
and
-
Response
. They map directly to the underlying HTTP concepts,
+
+Response
. They map directly to the underlying HTTP concepts,
but have
-
certain visibility filters in place for privacy and security reasons,
+
certain visibility filters in place for privacy and security reasons,
such as
-
supporting CORS rules and ensuring cookies aren’t readable by third parties.
+
supporting CORS rules and ensuring cookies aren’t readable by third parties.
The Headers interface is
a simple multi-map of names to values:
-
-
var content = "Hello World";
+ var content = "Hello World";
var reqHeaders = new Headers();
reqHeaders.append("Content-Type", "text/plain"
reqHeaders.append("Content-Length", content.length.toString());
reqHeaders.append("X-Custom-Header", "ProcessThisImmediately");
-
+
The same can be achieved by passing an array of arrays or a JS object
literal
-
to the constructor:
-
-
reqHeaders = new Headers({
+
to the constructor:
+ reqHeaders = new Headers({
"Content-Type": "text/plain",
"Content-Length": content.length.toString(),
"X-Custom-Header": "ProcessThisImmediately",
});
-
+
The contents can be queried and retrieved:
-
-
console.log(reqHeaders.has("Content-Type")); // true
+ console.log(reqHeaders.has("Content-Type")); // true
console.log(reqHeaders.has("Set-Cookie")); // false
reqHeaders.set("Content-Type", "text/html");
reqHeaders.append("X-Custom-Header", "AnotherValue");
-
+
console.log(reqHeaders.get("Content-Length")); // 11
console.log(reqHeaders.getAll("X-Custom-Header")); // ["ProcessThisImmediately", "AnotherValue"]
-
+
reqHeaders.delete("X-Custom-Header");
console.log(reqHeaders.getAll("X-Custom-Header")); // []
-
+
Some of these operations are only useful in ServiceWorkers, but they provide
-
a much nicer API to Headers.
+
a much nicer API to Headers.
Since Headers can be sent in requests, or received in responses, and have
various limitations about what information can and should be mutable, Headers
objects
have a guard property. This is not exposed to the Web, but
it affects which mutation operations are allowed on the Headers object.
-
Possible values are:
-
+ The details of how each guard affects the behaviors of the Headers object
are
-
in the specification. For example,
+
in the specification. For example,
you may not append or set a “request” guarded Headers’ “Content-Length”
header. Similarly, inserting “Set-Cookie” into a Response header is not
allowed so that ServiceWorkers may not set cookies via synthesized Responses.
All of the Headers methods throw TypeError if name
is not a
valid HTTP Header name. The mutation operations will throw TypeError
if there is an immutable guard. Otherwise they fail silently. For example:
-
-
var res = Response.error();
+ var res = Response.error();
try {
res.headers.set("Origin", "http://mybank.com");
} catch(e) {
console.log("Cannot pretend to be a bank!");
}
-
+
Request
@@ -146,34 +148,31 @@ console.log(reqHeaders.getAll(
The simplest Request is of course, just a URL, as you may do to GET a
resource.
-
-
var req = new Request("/index.html");
+ var req = new Request("/index.html");
console.log(req.method); // "GET"
console.log(req.url); // "http://example.com/index.html"
-
+
You may also pass a Request to the Request()
constructor to
create a copy.
-
(This is not the same as calling the clone()
method, which
+
(This is not the same as calling the clone()
method, which
is covered in
-
the “Reading bodies” section.).
-
-
var copy = new Request(req);
+
the “Reading bodies” section.).
+ var copy = new Request(req);
console.log(copy.method); // "GET"
console.log(copy.url); // "http://example.com/index.html"
-
+
Again, this form is probably only useful in ServiceWorkers.
The non-URL attributes of the Request
can only be set by passing
initial
-
values as a second argument to the constructor. This argument is a dictionary.
-
-
var uploadReq = new Request("/uploadImage", {
+
values as a second argument to the constructor. This argument is a dictionary.
+ var uploadReq = new Request("/uploadImage", {
method: "POST",
headers: {
"Content-Type": "image/png",
},
body: "image data"
});
-
+
The Request’s mode is used to determine if cross-origin requests lead
to valid responses, and which properties on the response are readable.
Legal mode values are "same-origin"
, "no-cors"
(default)
@@ -181,15 +180,14 @@ console.log(copy.url);
The "same-origin"
mode is simple, if a request is made to another
origin with this mode set, the result is simply an error. You could use
this to ensure that
-
a request is always being made to your origin.
-
-
var arbitraryUrl = document.getElementById("url-input").value;
+
a request is always being made to your origin.
+ var arbitraryUrl = document.getElementById("url-input").value;
fetch(arbitraryUrl, { mode: "same-origin" }).then(function(res) {
console.log("Response succeeded?", res.ok);
}, function(e) {
console.log("Please enter a same-origin URL!");
});
-
+
The "no-cors"
mode captures what the web platform does by default
for scripts you import from CDNs, images hosted on other domains, and so
on. First, it prevents the method from being anything other than “HEAD”,
@@ -202,20 +200,19 @@ fetch(arbitraryUrl, { mode:
"cors"
mode is what you’ll usually use to make known cross-origin
requests to access various APIs offered by other vendors. These are expected
to adhere to
-
the CORS protocol.
+
the CORS protocol.
Only a limited set of
headers is exposed in the Response, but the body is readable. For example,
you could get a list of Flickr’s most interesting photos
today like this:
-
-
var u = new URLSearchParams();
+ var u = new URLSearchParams();
u.append('method', 'flickr.interestingness.getList');
u.append('api_key', '<insert api key here>');
u.append('format', 'json');
u.append('nojsoncallback', '1');
-
+
var apiCall = fetch('https://api.flickr.com/services/rest?' + u);
-
+
apiCall.then(function(response) {
return response.json().then(function(json) {
// photo is a list of photos.
@@ -226,25 +223,26 @@ apiCall.then(function(respon
console.log(photo.title);
});
});
-
+
You may not read out the “Date” header since Flickr does not allow it
via
-
Access-Control-Expose-Headers
.
-
-
response.headers.get("Date"); // null
-
+
+Access-Control-Expose-Headers
.
+ response.headers.get("Date"); // null
+
The credentials
enumeration determines if cookies for the other
domain are
-
sent to cross-origin requests. This is similar to XHR’s withCredentials
-
flag, but tri-valued as "omit"
(default), "same-origin"
and "include"
.
+
sent to cross-origin requests. This is similar to XHR’s withCredentials
+
flag, but tri-valued as "omit"
(default), "same-origin"
and "include"
.
The Request object will also give the ability to offer caching hints to
the user-agent. This is currently undergoing some security review.
Firefox exposes the attribute, but it has no effect.
Requests have two read-only attributes that are relevant to ServiceWorkers
-
intercepting them. There is the string referrer
, which is
+
intercepting them. There is the string referrer
, which is
set by the UA to be
-
the referrer of the Request. This may be an empty string. The other is
-
context
which is a rather large enumeration defining
+
the referrer of the Request. This may be an empty string. The other is
+
+context
which is a rather large enumeration defining
what sort of resource is being fetched. This could be “image” if the request
is from an
<img>tag in the controlled document, “worker” if it is an attempt to load a
@@ -265,39 +263,40 @@ apiCall.then(function(respon
The url
attribute reflects the URL of the corresponding request.
Response also has a type
, which is “basic”, “cors”, “default”,
“error” or
-
“opaque”.
- "basic"
: normal, same origin response, with all headers exposed
+
“opaque”.
+
+ "basic"
: normal, same origin response, with all headers exposed
except
-
“Set-Cookie” and “Set-Cookie2″.
+
“Set-Cookie” and “Set-Cookie2″.
"cors"
: response was received from a valid cross-origin request.
Certain headers and the bodymay be accessed.
"error"
: network error. No useful information describing
the error is available. The Response’s status is 0, headers are empty and
immutable. This is the type for a Response obtained from Response.error()
.
"opaque"
: response for “no-cors” request to cross-origin
- resource. Severely
+ resource. Severely
restricted
-
The “error” type results in the fetch()
Promise rejecting with
+
+
The “error” type results in the fetch()
Promise rejecting with
TypeError.
There are certain attributes that are useful only in a ServiceWorker scope.
The
-
idiomatic way to return a Response to an intercepted request in ServiceWorkers
+
idiomatic way to return a Response to an intercepted request in ServiceWorkers
is:
-
-
addEventListener('fetch', function(event) {
+ addEventListener('fetch', function(event) {
event.respondWith(new Response("Response body", {
headers: { "Content-Type" : "text/plain" }
});
});
-
+
As you can see, Response has a two argument constructor, where both arguments
are optional. The first argument is a body initializer, and the second
is a dictionary to set the status
, statusText
and headers
.
The static method Response.error()
simply returns an error
response. Similarly, Response.redirect(url, status)
returns
a Response resulting in
-
a redirect to url
.
+
a redirect to url
.
Dealing with bodies
@@ -305,7 +304,8 @@ apiCall.then(function(respon
over it because of the various data types body may contain, but we will
cover it in detail now.
A body is an instance of any of the following types.
-
+ In addition, Request and Response both offer the following methods to
extract their body. These all return a Promise that is eventually resolved
with the actual content.
- arrayBuffer()
+
+ arrayBuffer()
blob()
@@ -331,21 +333,20 @@ apiCall.then(function(respon
formData()
-
This is a significant improvement over XHR in terms of ease of use of
+
+
This is a significant improvement over XHR in terms of ease of use of
non-text data!
Request bodies can be set by passing body
parameters:
-
-
var form = new FormData(document.getElementById('login-form'));
+ var form = new FormData(document.getElementById('login-form'));
fetch("/login", {
method: "POST",
body: form
})
-
+
Responses take the first argument as the body.
-
-
var res = new Response(new File(["chunk", "chunk"], "archive.zip",
+ var res = new Response(new File(["chunk", "chunk"], "archive.zip",
{ type: "application/zip" }));
-
+
Both Request and Response (and by extension the fetch()
function),
will try to intelligently determine the content type.
Request will also automatically set a “Content-Type” header if none is
@@ -356,18 +357,17 @@ fetch("/login", {
It is important to realise that Request and Response bodies can only be
read once! Both interfaces have a boolean attribute bodyUsed
to
determine if it is safe to read or not.
-
-
var res = new Response("one time use");
+ var res = new Response("one time use");
console.log(res.bodyUsed); // false
res.text().then(function(v) {
console.log(res.bodyUsed); // true
});
console.log(res.bodyUsed); // true
-
+
res.text().catch(function(e) {
console.log("Tried to read already consumed Response");
});
-
+
This decision allows easing the transition to an eventual stream-based Fetch
API. The intention is to let applications consume data as it arrives, allowing
for JavaScript to deal with larger files like videos, and perform things
@@ -381,22 +381,21 @@ res.text().catch(clone() MUST
be called before the body of the corresponding object has been used. That
is, clone()
first, read later.
-
-
addEventListener('fetch', function(evt) {
+ addEventListener('fetch', function(evt) {
var sheep = new Response("Dolly");
console.log(sheep.bodyUsed); // false
var clone = sheep.clone();
console.log(clone.bodyUsed); // false
-
+
clone.text();
console.log(sheep.bodyUsed); // false
console.log(clone.bodyUsed); // true
-
+
evt.respondWith(cache.add(sheep.clone()).then(function(e) {
return sheep;
});
});
-
+
Future improvements
@@ -409,7 +408,11 @@ res.text().catch(Fetch and
ServiceWorkerspecifications.
For a better web!
- The author would like to thank Andrea Marchesini, Anne van Kesteren and Ben
+ The author would like to thank Andrea Marchesini, Anne van Kesteren and Ben
Kelly for helping with the specification and implementation.
-