Snippets Collections
const url ='http://sample.example.file.doc'
const authHeader ="Bearer 6Q************" 

const options = {
  headers: {
    Authorization: authHeader
  }
};
 fetch(url, options)
  .then( res => res.blob() )
  .then( blob => {
    var file = window.URL.createObjectURL(blob);
    window.location.assign(file);
  });
fetch("https://free-nba.p.rapidapi.com/teams?page=0", {
	"method": "GET",
	"headers": {
		"x-rapidapi-host": "free-nba.p.rapidapi.com",
		"x-rapidapi-key": "d1e022a733msh4e2c4a1ea244bf9p1a3971jsnea127bfc9297"
	}
})
.then((response) => {
  return response.json() // << This is the problem
})
.then((responseData) => { // responseData = undefined
  console.log(responseData.data);
 
})
.catch(err => {
	console.error(err);
});
star

Fri May 03 2024 18:15:39 GMT+0000 (Coordinated Universal Time) https://stackoverflow.com/questions/32545632/how-can-i-download-a-file-using-window-fetch

#javascript #promise
star

Mon Aug 23 2021 18:14:18 GMT+0000 (Coordinated Universal Time)

#fetch #promise

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension