JavaScriptEasy
How do you make an HTTP request using the Fetch API?
By FrontendPro Editorial Team Updated 8/8/2026
#JavaScript
Answer
To make an HTTP request using the Fetch API, you can use the fetch function, which returns a promise. You can handle the response using .then() and .catch() for error handling. Here's a basic example of a GET request:
js
fetch("https://jsonplaceholder.typicode.com/todos/1")
.then((response) => response.json())
.then((data) => console.log(data))
.catch((error) => console.error("Error:", error));
For a POST request, you can pass an options object as the second argument to fetch:
js
fetch("https://jsonplaceholder.typicode.com/posts", {
method: "POST",
body: JSON.stringify({
title: "foo",
body: "bar",
userId: 1,
}),
headers: {
"Content-Type": "application/json; charset=UTF-8",
},
})
.then((response) => response.json())
.then((data) => console.log(data))
.catch((error) => console.error("Error:", error));
