GET请求在POSTMAN和Chrome中成功,但在axios中失败

ni65a41a  于 2022-11-05  发布在  iOS
关注(0)|答案(2)|浏览(497)

我尝试从GOAT网站获取数据,但遇到问题:GET请求在POSTMAN和Chrome中成功,但在axios中失败。('error code:1020').所以我想这是因为Cloudflare,但我想知道为什么它在POSTMAN上工作,我怎么能用axios做到这一点. URL:

'https://www.goat.com/web-api/v1/product_variants/buy_bar_data?productTemplateId=815559'

我的程式码(从 Postman 程式码片段复制):

var axios = require('axios');

var config = {
  method: 'get',
  url: 'https://www.goat.com/web-api/v1/product_variants/buy_bar_data?productTemplateId=815559',
};

axios(config)
.then(function (response) {
  console.log(JSON.stringify(response.data));
})
.catch(function (error) {
  console.log(error);
});
bnlyeluc

bnlyeluc1#

看起来这个网站正在使用cloudflare。请尝试在您的axios请求中添加标题,例如:

const axios = require('axios');

let config = {
    method: 'GET',
    url: 'https://www.goat.com/web-api/v1/product_variants/buy_bar_data?productTemplateId=815559',
    headers: {
        "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
        "accept-language": "en-US,en;q=0.9",
        "cache-control": "no-cache",
        "pragma": "no-cache",
        "sec-ch-ua": "\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"101\", \"Google Chrome\";v=\"101\"",
        "sec-ch-ua-mobile": "?0",
        "sec-ch-ua-platform": "\"Windows\"",
        "sec-fetch-dest": "document",
        "sec-fetch-mode": "navigate",
        "sec-fetch-site": "none",
        "sec-fetch-user": "?1",
        "upgrade-insecure-requests": "1",
    }
};

axios(config).then(async (response) => {
    let data = response.data;
    console.log(data);
}).catch((error) => {
    console.log(error);
});

这应该可以解决您的问题!

相关问题