1fetch('http://example.com/songs')
2 .then(response => response.json())
3 .then(data => console.log(data))
4 .catch(err => console.error(err));
1fetch('https://api.github.com/users/manishmshiva', {
2 method: "GET",
3 headers: {"Content-type": "application/json;charset=UTF-8"}
4})
5.then(response => response.json())
6.then(json => console.log(json));
7.catch(err => console.log(err));
1fetch('./yourjson.json')
2 .then((response) => response.json())
3 .then((data) => {
4 console.log(data);
5 })
1// This will fetch api.example.com/comments with a header and a body
2fetch(`https://api.example.com/comments`, {
3 method: 'POST', //This could be any http method
4 headers: {
5 'Authorization': 'Basic SGVsbG8gdGhlcmUgOikgSGF2ZSBhIGdvb2QgZGF5IQ==',
6 'Content-Type': 'application/json',
7 },
8 body: JSON.stringify({
9 UID: 58,
10 Comment: "Fetch is really easy!",
11 }),
12})
13.then((response) => response.json())
14.then((newComment) => {
15 // Do something magical with your newly posted comment :)
16});
1fetch('http://example.com/movies.json')
2 .then((response) => {
3 return response.json();
4 })
5 .then((data) => {
6 console.log(data);
7 });
8
1fetch(url)
2 .then(response => {
3 // handle the response
4 })
5 .catch(error => {
6 // handle the error
7 });