JavaScript Fetch API
The Fetch API interface lets you download (fetch) data from a web server.
The Fetch API interface lets you download (fetch) data from a web server.
await fetch() is the modern API for fetching data from an HTTP server.
XMLHttpRequest() was the old legacy API for fetching data from an HTTP server.
Share
What is Fetch API
JavaScript Fetch API is a modern browser interface used to make asynchronous network requests to web servers.
Fetch is an easier and more powerful replacement for the older XMLHttpRequest object.
Fetch is uses standard JavaScript Promises for cleaner asynchronous code.
Fetching a Text File
The simplest way to demonstrate fetch() is to download a text file.
Examples
Example Code
javascript// Fetch a file
fetch(file)
.then(function(response) {
return response.text();
})
.then(function(data) {
myDisplayer(data);
});Using an Asynchronous Function
Example Code
javascript// Async function to fetch a file
async function loadText(file) {
const response = await fetch(file);
myDisplayer(await response.text());
}The Response Object
The fetch() method returns a Promise that resolves to a Response object.
The Response object contains information (properties) about the server response.
| Property | Description |
|---|---|
| ok | True if the request succeeded |
| status | The HTTP status code |
| statusText | The HTTP status message |
| url | The HTTP url address |
Example
Example Code
javascript// Async function to download a file
async function loadText(file) {
const response = await fetch(file);
myDisplayer(response);
}The ok Property
Example
Example Code
javascript// Async function to download a file
async function loadText(file) {
const response = await fetch(file);
myDisplayer(response.ok);
}The status Property
Example
Example Code
javascript// Async function to download a file
async function loadText(file) {
const response = await fetch(file);
myDisplayer(response.status);
}The url Property
Example
Example Code
javascript// Async function to download a file
async function loadText(file) {
const response = await fetch(file);
myDisplayer(response.url);
}Asynchrounous Output
JavaScript can continue running while waiting for the server to respond.
Example
Example Code
javascript// Async function to download a file
async function loadText(file) {
const response = await fetch(file);
myDisplayer(response.url);
}
loadText("fetch.txt");
myDisplayer("JavaScript continues.");Response Object Methods
The Response object has methods to read the server response.
| Method | Description |
|---|---|
| text() | Reads the response as text |
| json() | Reads the response as JSON |
| blob() | Reads the response as binary data |
| bytes() | Reads the response as bytes |
| arrayBuffer() | Reads the response as an ArrayBuffer |
Checking for HTTP Errors
fetch() resolves when the server responds.
A common beginner mistake is expecting fetch() to fail on Http errors.
An HTTP error such as 404 Not Found does not reject the Promise.
Fetch only rejects on network errors.
A 404 response is not a rejected promise.
You should always check the ok property.
Example
Example Code
javascriptasync function loadText(file) {
const response = await fetch(file);
if (!response.ok) {
myDisplayer(response.status + " " + response.statusText);
return;
}
myDisplayer(await response.text());
}Fetch API vs. XMLHttpRequest (XHR)
| Feature | Fetch | XHR |
|---|---|---|
| Syntax | Promise-based | Callback-based |
| Error Handling | Rejects on network failure | Needs manual checking |
| Streams | Supports streams | Does not support streams |
Interactive Code Examples
Examples
javascript// Fetch a file
fetch(file)
.then(function(response) {
return response.text();
})
.then(function(data) {
myDisplayer(data);
});Using an Asynchronous Function
javascript// Async function to fetch a file
async function loadText(file) {
const response = await fetch(file);
myDisplayer(await response.text());
}JS Example 3
javascript// Async function to download a file
async function loadText(file) {
const response = await fetch(file);
myDisplayer(response);
}JS Example 4
javascript// Async function to download a file
async function loadText(file) {
const response = await fetch(file);
myDisplayer(response.ok);
}JS Example 5
javascript// Async function to download a file
async function loadText(file) {
const response = await fetch(file);
myDisplayer(response.status);
}JS Example 6
javascript// Async function to download a file
async function loadText(file) {
const response = await fetch(file);
myDisplayer(response.url);
}