learn fetch api javascript code example

Example 1: fetch api tutorial

fetch('https://api.github.com/users/manishmshiva', {
  method: "GET",
  headers: {"Content-type": "application/json;charset=UTF-8"}
})
.then(response => response.json()) 
.then(json => console.log(json)); 
.catch(err => console.log(err));

Example 2: fetch api javascript

fetch('https://apiYouWantToFetch.com/players') // returns a promise
  .then(response => response.json()) // converting promise to JSON
  .then(players => console.log(players)) // console.log to view the response

Example 3: fetch api in javascript

// GET Request.
fetch('https://jsonplaceholder.typicode.com/users')
    // Handle success
    .then(response => response.json())  // convert to json
    .then(json => console.log(json))    //print data to console
    .catch(err => console.log('Request Failed', err)); // Catch errors

Example 4: fetch suntax

fetch('http://example.com/movies.json')
  .then((response) => {
    return response.json();
  })
  .then((data) => {
    console.log(data);
  });

Tags:

Misc Example