The Fetch API provides a JavaScript interface that enables users to manipulate and access parts of the HTTP pipeline such as responses and requests.
Fetch API has so many rich and exciting options like method, headers, body, referrer, mode, credentials, cache, redirect, integrity, and a few more. However, the most prominent ones we use are method, headers, and body.
The method is used to create, read, update and delete data, and hence we have methods like GET, POST, PUT and DELETE.
As an example we would use the API: https://reqres.in/arpi/users which would give us random data about a person, our goal here is to see what is presented in this data.
Syntax:
fetch(URL, options)
Parameters: This method accepts two parameters as shown above and described below:
- URL: This is the endpoint of the resource from where you want to fetch data.
- Options: This is an optional parameter, it is an options object that can have the following values:
- Method: This denotes the method type, it can be of any HTTP request type, like GET, POST, PUT, DELETE, etc.
- Headers: In case we are passing data to the server, we need to additionally tell fetch that we are going to pass data in form of json/text, etc.
- Body: In this part, we actually pass the data as a JSON.
Example 1: GET Request demonstration.
Javascript
.then(res => res.json())
.then(data => console.log(data))
|
Output: This is what the API is returning to us, some random data about people.
{page: 1, per_page: 6, total: 12, total_pages: 2, data: Array(6), …}
data
:(6) [{…}, {…}, {…}, {…}, {…}, {…}]
page
:1
per_page
:6
support
:{url:
'https://reqres.in/#support-heading', text: 'To keep ReqRes free,
contributions towards server costs are appreciated!'}
total
:12
total_pages
:2
[[Prototype]]
:Object
Example 2: POST Request demonstration. We will now post some random data by ourselves, with the help of the POST method.
Javascript
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
name: 'Geeks For Geeks',
})
}).then(res => {
return res.json()
}).then(data => console.log(data));
|
Output: Here, we see that we have successfully made a POST request using fetch API.
{name:
'Geeks For Geeks', website: 'https://www.geeksforgeeks.org/',
id: '96',
createdAt: '2023-01-11T06:53:03.680Z'}
Learn to code easily with our course
Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
12 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...