fetch('https://example.com/path',
{method:'GET',
headers: {
'Authorization': 'Basic ' + btoa('login:password') //use btoa in js and Base64.encode in node
}
})
.then(response => response.json())
.then(json => console.log(json));
fetch(url)
.then(response => {
// handle the response
})
.catch(error => {
// handle the error
});
fetch('https://shazam.p.rapidapi.com/search?term=kiss%20the%20rain&locale=en-US&offset=0&limit=5', {
// request method
method: 'GET',
// headers from the API documentation
headers: {
'X-RapidAPI-Key': '8bd90c4cffmsh2788964981ec641p113417jsn3d0aff3880f5',
'X-RapidAPI-Host': 'shazam.p.rapidapi.com'
}
})
.then((result) => result.json()) // result from API endpoint
.then((data) => console.log(data)) // result in json format
.catch((error) => console.log(error)); // catching the error should it occur
async function fetchText() {
let response = await fetch('/readme.txt');
let data = await response.text();
console.log(data);
}Code language: JavaScript (javascript)
fetch(file)
.then(x => x.text())
.then(y => myDisplay(y));
fetch()
fectch()