using fetch in API code example
Example 1: fetch api in javascript
fetch('https://jsonplaceholder.typicode.com/users')
.then(response => response.json())
.then(json => console.log(json))
.catch(err => console.log('Request Failed', err));
Example 2: fetch api javascript
fetch(`https://api.example.com/comments`, {
method: 'POST',
headers: {
'Authorization': 'Basic SGVsbG8gdGhlcmUgOikgSGF2ZSBhIGdvb2QgZGF5IQ==',
'Content-Type': 'application/json',
},
body: JSON.stringify({
UID: 58,
Comment: "Fetch is really easy!",
}),
})
.then((response) => response.json)
.then((newComment) => {
});
Example 3: fetch api template
const fetchData=(inputs)=>{
fetch('https://example.com/api', {
method: 'post',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(inputs)
})
.then(function(response) {
return response.json();
})
.then(function(data) {
ChromeSamples.log(data.whatever);
});
}