Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
August 18, 2021 12:03 am GMT

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

To Contact Me:

Happy codding!


Original Link: https://dev.to/ayabouchiha/sending-get-request-using-fetch-5fie

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To