简体   繁体   中英

403 error on axios head request

When I make a head request from axios to this url , it returns a 403 Forbidden response. It works fine when I make a get request or even when I make a head request from other REST clients like postman. I am guessing it's because CloudFront is trying to deny access from bots. Can I do some workaround so I can get this request to work properly?

const axios = require('axios')
axios.head('https://images-na.ssl-images-amazon.com/images/I/415%2BC2ZrYqL.jpg')
  .then(console.log)
  .catch(console.log)

I dont see a problem in getting the image from stack snippet. Not sure why you are getting 403

 axios.get('https://images-na.ssl-images-amazon.com/images/I/415%2BC2ZrYqL.jpg', { responseType: 'arraybuffer' }) .then(response => { console.log(response.data); var arrayBuffer = response.data; var bytes = new Uint8Array(arrayBuffer); var image = document.getElementById('image'); image.src = "data:image/jpg;base64," + encode(bytes); }); // public method for encoding an Uint8Array to base64 function encode (input) { var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; var output = ""; var chr1, chr2, chr3, enc1, enc2, enc3, enc4; var i = 0; while (i < input.length) { chr1 = input[i++]; chr2 = i < input.length ? input[i++] : Number.NaN; // Not sure if the index chr3 = i < input.length ? input[i++] : Number.NaN; // checks are needed here enc1 = chr1 >> 2; enc2 = ((chr1 & 3) << 4) | (chr2 >> 4); enc3 = ((chr2 & 15) << 2) | (chr3 >> 6); enc4 = chr3 & 63; if (isNaN(chr2)) { enc3 = enc4 = 64; } else if (isNaN(chr3)) { enc4 = 64; } output += keyStr.charAt(enc1) + keyStr.charAt(enc2) + keyStr.charAt(enc3) + keyStr.charAt(enc4); } return output; } 
 <script src="https://unpkg.com/axios/dist/axios.min.js"></script> <img id="image"/> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM