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

South Korea bans downloads of DeepSeek AI until further notice

The South Korean government announced on Monday that it had temporarily halted new downloads of…

4 days ago

Which programming language is best for software development?

As a software developer, choosing the right programming language for software development can be a…

7 days ago

What is a server farm?

A server farm, also known as a server cluster or data center, is a collection…

1 week ago

Elon Musk’s Starlink satellite internet service is awaiting authorization to begin operations in Pakistan.

Pakistan's mobile and broadband internet speeds rank in the bottom 10 percent globally, according to…

1 week ago

React Native Styling Guide

React Native is a popular framework for building cross-platform mobile applications using JavaScript and React.…

2 weeks ago

You can now customize your Android home screen shortcut with any widget of your choice

Google is not only passionate about developing innovative apps and services but also about finding…

2 weeks ago