JavaScript tutorials > Web APIs and the DOM > Browser APIs > What is the Fetch API?
What is the Fetch API?
The Fetch API is a modern interface for making network requests in JavaScript. It provides a more powerful and flexible alternative to the older With the Fetch API, you can retrieve resources from a server, send data to a server, and handle the responses in a streamlined manner. It's a fundamental tool for building web applications that need to interact with external APIs or servers.XMLHttpRequest
(XHR) object. The Fetch API uses Promises, which makes asynchronous operations easier to manage and read.
Basic Fetch Request
This snippet demonstrates a simple GET request to https://api.example.com/data
. Let's break it down:fetch('https://api.example.com/data')
initiates the request..then(response => { ... })
handles the response. It first checks if the response was successful (response.ok
). If not, it throws an error.response.json()
parses the response body as JSON and returns another Promise..then(data => { ... })
receives the parsed JSON data and logs it to the console..catch(error => { ... })
catches any errors that occurred during the fetch operation.
fetch('https://api.example.com/data')
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json();
})
.then(data => {
console.log(data);
})
.catch(error => {
console.error('Fetch error:', error);
});
Concepts Behind the Snippet
The key concepts in the Fetch API are:response
object returned by fetch()
contains metadata about the response, such as the HTTP status code, headers, and the response body.Request
object to configure the request in more detail (e.g., setting headers, method, body).
Real-Life Use Case: Fetching User Data
This example demonstrates fetching user data from a hypothetical endpoint /users/12345
and displaying it on a web page. It retrieves user data as JSON and populates a div
element with the user's name and email.
fetch('/users/12345')
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(user => {
const userDetailsDiv = document.getElementById('userDetails');
userDetailsDiv.innerHTML = `
<h2>${user.name}</h2>
<p>Email: ${user.email}</p>
`;
})
.catch(error => {
console.error('There was a problem fetching user data:', error);
});
Making a POST Request
This snippet shows how to send a POST request to a server. Here's what's happening:fetch()
is an options object that allows you to configure the request.method: 'POST'
specifies the HTTP method.headers
sets the request headers. Content-Type: 'application/json'
indicates that the request body is in JSON format.body
contains the data to be sent. JSON.stringify()
converts the JavaScript object into a JSON string.
fetch('/api/submit-form', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
name: 'John Doe',
email: 'john.doe@example.com'
})
})
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
console.log('Success:', data);
})
.catch(error => {
console.error('Error:', error);
});
Best Practices
Here are some best practices to follow when using the Fetch API:.catch()
method to gracefully handle network issues or server errors.response.ok
) before parsing the response body.Content-Type
header when sending data.async/await
syntax for a more readable and maintainable alternative to Promise chains.
Async/Await Example
This code provides the same functionality as the first example but uses the async/await
syntax. This makes the code more linear and easier to understand.async
keyword indicates that the function will contain asynchronous operations.await
keyword pauses the execution of the function until the Promise resolves.
async function fetchData() {
try {
const response = await fetch('https://api.example.com/data');
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
console.log(data);
} catch (error) {
console.error('Fetch error:', error);
}
}
fetchData();
Interview Tip
When discussing the Fetch API in an interview, be sure to mention:XMLHttpRequest
(e.g., Promise-based, more modern API).async/await
for improved readability.
When to Use the Fetch API
The Fetch API is suitable for virtually any situation where you need to make HTTP requests from a web browser. Use it when:XMLHttpRequest
.
Alternatives to Fetch API
While Fetch API is now the standard, there are alternatives:$.ajax()
method provides a convenient way to make HTTP requests.
Pros of Fetch API
The Fetch API offers several advantages:XMLHttpRequest
.
Cons of Fetch API
The Fetch API also has some limitations:response.ok
). Doesn't automatically reject promises for HTTP error codes (400, 500, etc.).credentials
option.
FAQ
-
Does the Fetch API handle cross-origin requests (CORS)?
Yes, the Fetch API supports CORS. Browsers enforce CORS restrictions to prevent malicious websites from accessing data from other domains. The server must include appropriate CORS headers in its responses to allow cross-origin requests from your website. If the server doesn't provide the correct headers, the browser will block the request.
-
How do I set custom headers in a Fetch request?
You can set custom headers in the options object passed to the
fetch()
function. For example:fetch('/api/data', { method: 'GET', headers: { 'Authorization': 'Bearer YOUR_API_KEY', 'Custom-Header': 'Custom Value' } }) .then(response => { ... });
This example sets an
Authorization
header with an API key and a custom header namedCustom-Header
. -
How do I handle different response types (e.g., JSON, text, blob)?
The
response
object provides methods to parse the response body in different formats:response.json()
: Parses the response as JSON.response.text()
: Parses the response as plain text.response.blob()
: Parses the response as a Blob (binary data).response.formData()
: Parses the response as FormData.response.arrayBuffer()
: Parses the response as ArrayBuffer.
Choose the appropriate method based on the expected response type from the server.