问题
I have setup my axios like this:
const agent = new https.Agent({
rejectUnauthorized: false
});
and sending a get call like this:
let data = await axios.get('https://www.skechers.com/en-us/', {
httpsAgent: agent
});
but with some urls my request fails with this error:
Request failed with status code 403
what would be the possible reason to cause this error. I have tried setting up headers as follow but still getting the error
let data = await axios.get(url, {
httpsAgent: agent,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': '*'
}
});
回答1:
I guess it has something to do with CSRF cookie not being sent when you are using axios. You can consider two approach
Either use axios-cookiejar-support to add while making the request
OR use got which provides this inbuilt.
so your code will be simply
const got = require("got");
(async () => {
console.log(await got.get("https://www.skechers.com/en-us/"));
})();
来源:https://stackoverflow.com/questions/60636207/getting-request-failed-with-status-code-403-with-axios-get