How to use the Fetch API in JavaScript

I'm a full stack developer based in Australia. I write articles about Node.js, React, JavaScript & Web Development to make your learning journey easier ๐
Search for a command to run...

I'm a full stack developer based in Australia. I write articles about Node.js, React, JavaScript & Web Development to make your learning journey easier ๐
No comments yet. Be the first to comment.
Introduction Website performance is an important aspect of web development. It consists of mainly two things: The speed of your website How usable is your website for visitors? Importance of website performance Website performance is very importa...

There are plenty of native web APIs that have a variety of uses. Here are 5 useful APIs that I use quite often in my web development projects. Clipboard API The clipboard API is a simple method to copy text to a user's clipboard. This can be used in ...

Hey All ๐ In this article, we'll be covering 4 mistakes commonly made by people while learning to code that will seriously impact their coding journey. #1 - Copying and Pasting The first mistake I'll talk about is copying and pasting. We've all don...

Introduction There are plenty of units in CSS. From px to vh, there is an abundance of units all with their distinctive use cases. Considering the amount of options, its perfectly reasonably to be confused as to which unit you should use and where. T...

In this article, we'll be covering the Fetch API in JavaScript how you can use it to make HTTP requests.
The fetch() method provides a simple way to make HTTP requests in JavaScript.
It is natively in the browser, meaning you don't have to import an additional module (like axios).
Additionally, its asynchronous and promise-based which makes catching errors and handling responses easy.
The syntax is very simple, you just have to use the fetch() method
Making a GET request is very simple:
fetch('request url')
Replace request url with the URL you want tor request.
The fetch method returns a promise, which means there are two ways you can handle the response.
.then Syntaxfetch('request url...')
.then(response => response.json())
.then(json => console.log(json))
.catch(error => console.error(error));
async/awaitasync function makeRequest() {
const response = await fetch('request url...');
const json = await response.json();
console.log(json);
}
We can improve the async/await further by wrapping it in a try/catch statement to catch an error:
async function makeRequest() {
try {
const response = await fetch('request url...');
const json = await response.json();
console.log(json);
} catch (error) {
console.error(error)
}
}
You can add options to the request by adding a second parameter.
This allows you to add headers, change the request type, and more.
For example, to make a post request:
fetch("https://example.com/profile", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(data),
});
The method can be set as any HTTP Request method like GET, POST, PUT, DELETE, etc.
The headers object is used to control the headers of the request.
This could be used for authenticating requests, changing the content type (like in the request above), or other headers you want to include.
The body is the request's body.
That is a overview of how the Fetch API works in JavaScript.
It can used as an alternative to popular packages like axios, as well as built in methods like XMLHttpRequest
๐ Thanks for reading this article!
If you like what I do and would love to see more related content, follow me on my other social platforms:
Twitter: Blake Yeboah
GitHub: Blake-K-Yeboah
LinkedIn: Blake Yeboah