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));
1//Obj of data to send in future like a dummyDb
2const data = { username: 'example' };
3
4//POST request with body equal on data in JSON format
5fetch('https://example.com/profile', {
6 method: 'POST',
7 headers: {
8 'Content-Type': 'application/json',
9 },
10 body: JSON.stringify(data),
11})
12.then((response) => response.json())
13//Then with the data from the response in JSON...
14.then((data) => {
15 console.log('Success:', data);
16})
17//Then with the error genereted...
18.catch((error) => {
19 console.error('Error:', error);
20});
21
22//
1fetch('http://example.com/movies.json')
2 .then((response) => {
3 return response.json();
4 })
5 .then((myJson) => {
6 console.log(myJson);
7 });
1fetch('https://jsonplaceholder.typicode.com/todos/1')
2 .then(response => response.json())
3 .then(json => console.log(json))
1// fetch API
2var myData = async () => {
3 try {
4 const raw_response = await fetch("https://jsonplaceholder.typicode.com/users");
5 if (!raw_response.ok) { // check for the 404 errors
6 throw new Error(raw_response.status);
7 }
8 const json_data = await raw_response.json();
9 console.log(json_data);
10 }
11 catch (error) { // catch block for network errors
12 console.log(error);
13 }
14}
15fetchUsers();