简体   繁体   English

axios head请求上出现403错误

[英]403 error on axios head request

When I make a head request from axios to this url , it returns a 403 Forbidden response. 当我从axios向此url发出头请求时,它返回403 Forbidden响应。 It works fine when I make a get request or even when I make a head request from other REST clients like postman. 当我发出get请求或什至从其他REST客户端(如邮递员)发出头请求时,它都可以正常工作。 I am guessing it's because CloudFront is trying to deny access from bots. 我猜这是因为CloudFront试图拒绝来自机器人的访问。 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 不知道为什么得到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"/> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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