Blog Post

Tags

How to use the Fetch API with JavaScript

When it comes to making HTTP requests inside of JavaScript, you can use a variety of different options. For example, you can use XMLHttpRequest, a third party library like Axios, or more recently, Fetch.

What exactly is Fetch?

Fetch is a promise based HTTP request API built into JavaScript. It's fairly well supported (87%+) and is promise based for ease of use and is a perfect fit for those wanting to use out of the box solutions for HTTP.

Let's take a look at a few different ways we can use fetch to access data from the JSON Placeholder API:

Get

1. We're calling `fetch` with an API and this defaults to a GET request, returning a promise.
2. This returns us a HTTP Response, and as we're only interested in the **body** of that response, we call res.json() to extract this.
3. As we now have the **data** from our API call, we can perform any action we want on it.
When it comes to other HTTP verbs such as POST/PUT/DElETE, the process is quite similar:

Customisation

Let's use fetch to create a new post by using HTTP POST. This starts by creating the actual post:

WRITTEN BY PAUL HALLIDAY
Learn a little more about Developer Advocate Paul Halliday and see his available blogs and courses on his website.

Who Are Ronald James?

We are a leading niche digital & tech recruitment specialist for the North East of England. We Specialise in the acquisition of high-performing technology talent across a variety of IT sectors including Digital & Technology Software Development.

Our ultimate goal is to make a positive impact on every client and candidate we serve - from the initial call and introduction, right up to the final delivery, we want our clients and candidates to feel they have had a beneficial and productive experience.

Contact our Team

If you’re looking to start your journey in sourcing talent or find your dream job, you’ll need a passionate, motivated team of experts to guide you. Check out our Jobs page for open vacancies. If interested, contact us or call 0191 620 0123 for a quick chat with our team.