This is an experimental technology
Because this technology's specification has not stabilized, check the compatibility table for usage in various browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future versions of browsers as the specification changes.
The Response interface of the Fetch API represents the response to a request.
You can create a new Response object using the Response.Response() constructor, but you are more likely to encounter a Response object being returned as the result of another API operation, for example a service worker Fetchevent.respondWith, or a simple GlobalFetch.fetch().
Constructor
Response.Response()- Creates a new
Responseobject.
Properties
Response.typeRead only- Contains the type of the response (e.g.,
basic,cors). Response.urlRead only- Contains the URL of the response.
Response.useFinalURL- Contains a boolean stating whether this is the final URL of the response.
Response.statusRead only- Contains the status code of the response (e.g.,
200for a success). Response.okRead only- Contains a boolean stating whether the response was successful (status in the range 200-299) or not.
Response.statusTextRead only- Contains the status message corresponding to the status code (e.g.,
OKfor200). Response.headersRead only- Contains the
Headersobject associated with the response.
Response implements Body, so it also has the following property available to it:
Body.bodyUsedRead only- Stores a
Booleanthat declares whether the body has been used in a response yet.
Methods
Response.clone()- Creates a clone of a
Responseobject. Response.error()- Returns a new
Responseobject associated with a network error. Response.redirect()- Creates a new response with a different URL.
Response implements Body, so it also has the following methods available to it:
Body.arrayBuffer()- Takes a
Responsestream and reads it to completion. It returns a promise that resolves with anArrayBuffer. Body.blob()- Takes a
Responsestream and reads it to completion. It returns a promise that resolves with aBlob. Body.formData()- Takes a
Responsestream and reads it to completion. It returns a promise that resolves with aFormDataobject. Body.json()- Takes a
Responsestream and reads it to completion. It returns a promise that resolves with aJSONobject. Body.text()- Takes a
Responsestream and reads it to completion. It returns a promise that resolves with aUSVString(text).
Examples
In our basic fetch example (run example live) we use a simple fetch() call to grab an image and display it in an <img> tag. The fetch() call returns a promise, which resolves with the Response object associated with the resource fetch operation. You'll notice that since we are requesting an image, we need to run Body.blob (Response implements body) to give the response its correct MIME type.
var myImage = document.querySelector('.my-image');
fetch('flowers.jpg').then(function(response) {
return response.blob();
}).then(function(response) {
var objectURL = URL.createObjectURL(response);
myImage.src = objectURL;
});
You can also use the Response.Response() constructor to create your own custom Response object:
var myResponse = new Response();
Specifications
| Specification | Status | Comment |
|---|---|---|
| Fetch The definition of 'Response' in that specification. |
Living Standard | Initial definition |
Browser compatibility
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
|---|---|---|---|---|---|
| Basic support | 42 41[1] |
39 (39) 34[1] |
No support |
29 |
No support |
| Feature | Android | Firefox Mobile (Gecko) | Firefox OS (Gecko) | IE Phone | Opera Mobile | Safari Mobile | Chrome for Android |
|---|---|---|---|---|---|---|---|
| Basic support | No support | No support | No support | No support | No support | No support | No support |
[1] This feature is implemented behind a preference.