An Interest In:
Web News this Week
- April 27, 2024
- April 26, 2024
- April 25, 2024
- April 24, 2024
- April 23, 2024
- April 22, 2024
- April 21, 2024
August 18, 2021 12:03 am GMT
Original Link: https://dev.to/ayabouchiha/sending-get-request-using-fetch-5fie
Sending GET Request Using Fetch
Hello everybody, today, we'll discuss sending GET requests using fetch;
What's GET request
GET: is a request used for getting or retrieving data or information from a specified server.
Code using then and catch
const getTodo = (id) => { const url = `https://jsonplaceholder.typicode.com/todos/${id}`; fetch(url) .then((response) => response.json()) .then((todo) => console.log(todo)) .catch((e) => console.log('something went wrong ;(', e));};getTodo(1);
Code using async and await
Method 1
const getTodo = async (id) => { const url = `https://jsonplaceholder.typicode.com/todos/${id}`; try { const response = await fetch(url); const data = await response.json(); console.log(data); } catch (e) { console.log('something went wrong :(', e); }};getTodo(1);
Method 2
const getTodo = async (id) => { const url = `https://jsonplaceholder.typicode.com/todos/${id}`; try { const data = await (await fetch(url)).json(); console.log(data); } catch (e) { console.log('something went wrong :(', e); }};getTodo(1);
Suggested Posts
- Making GET And POST Request Using Axios In React.js
- Making PUT & DELETE Request Using Axios In React.js
- Youtube Courses, Projects To Master Javascript
- Your Essential Guide To Map Built-in Object In Javascript
- All JS String Methods In One Post!
To Contact Me:
- email: [email protected]
- telegram: Aya Bouchiha
Happy codding!
Original Link: https://dev.to/ayabouchiha/sending-get-request-using-fetch-5fie
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To