1fetch('https://api.github.com/users/kipngetich33')
2 .then(response => response.json())
3 .then(formatedResponse => console.log(formatedResponse))
4//NB please replace url with your own
1// GET Request.
2fetch('https://jsonplaceholder.typicode.com/users')
3 // Handle success
4 .then(response => response.json()) // convert to json
5 .then(json => console.log(json)) //print data to console
6 .catch(err => console.log('Request Failed', err)); // Catch errors
1const url = 'https://randomuser.me/api';
2// The data we are going to send in our request
3let data = {
4 name: 'Sara'
5}
6// The parameters we are gonna pass to the fetch function
7let fetchData = {
8 method: 'POST',
9 body: data,
10 headers: new Headers()
11}
12fetch(url, fetchData)
13.then(function() {
14 // Handle response you get from the server
15});
1fetch('http://example.com/movies.json')
2 .then((response) => {
3 return response.json();
4 })
5 .then((data) => {
6 console.log(data);
7 });
8
1fetch('/about').then(function (response) {
2 // The API call was successful!
3 return response.text();
4}).then(function (html) {
5
6 // Convert the HTML string into a document object
7 var parser = new DOMParser();
8 var doc = parser.parseFromString(html, 'text/html');
9
10 // Get the image file
11 var img = doc.querySelector('img');
12 console.log(img);
13
14}).catch(function (err) {
15 // There was an error
16 console.warn('Something went wrong.', err);
17});
18