javascript

How to Use Fetch API in Javascript

In this tutorial, we are going to use the Fetch API to fetch all public holidays in the United States.

We’ve have written extensively on the Fetch API here.

So, let’s begin.

  getUsHolidays = async () => {
   const url = "https://date.nager.at/api/v2/publicholidays/2020/US";
    const obj = {
      method: "GET",
      headers: {
        Accept: "application/json",
        "Content-Type": "application/json",
      },
    };
    await fetch(`${url}`, obj)
      .then((response) => response.json())
      .then((responseJson) => {
        console.warn(responseJson);

      })
      .catch((error) => {
      console.error(error.message);
      });
  }

Explanation:

Here, we’re using async/await with GET request method, and the content type is application/json.

We’re getting the response on the console and we’re also catching errors as well because, should the request not go through, we want to know what happened.

Result:

Fetch API in Javascript

Conclusion:

Here’s how we can make a simple GET request using Fetch API in Javascript. Let me know what you think in the comments.

check out the official documentaton here

Recent Posts

Service Workers in JavaScript: An In-Depth Guide

Service Workers are one of the core features of modern web applications, offering powerful capabilities…

1 week ago

What are Database Driven Websites?

A database-driven website is a dynamic site that utilizes a database to store and manage…

2 weeks ago

How to show Toast Messages in React

Toasts are user interface elements commonly used in software applications, especially in mobile app development…

2 weeks ago

Exploring the Relationship Between JavaScript and Node.js

JavaScript has long been synonymous with frontend web development, powering interactive and dynamic user interfaces…

3 weeks ago

Key Differences Between Tailwind CSS and CSS3

Introduction: In the world of web development, CSS (Cascading Style Sheets) plays a crucial role…

3 weeks ago

Why Everybody is Learning JavaScript Right Now

JavaScript has emerged as a ubiquitous programming language, powering the modern web and extending its…

3 weeks ago