获取请求在邮递员和镀铬中成功,但Axios失败了

发布于 2025-02-03 13:12:23 字数 631 浏览 3 评论 0 原文

我正在尝试从山羊网站获取数据,但我有一个问题:在Postman和Chrome中获得请求成功,但Axios失败了。 ('错误代码: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);
});

I'm trying to get data from GOAT website but I have a problem : GET request succeeds in POSTMAN and Chrome but fails with axios. ('error code: 1020').
So I guess it's because of Cloudflare but I would like to know why it works on POSTMAN and how can I do it with axios.
URL :

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

My code (copied from postman code snippet) :

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);
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

箹锭⒈辈孓 2025-02-10 13:12:23

看起来这个网站正在使用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);
});

这应该解决您的问题!

it looks like this website is using cloudflare. Try adding headers to your axios request such as:

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);
});

This should resolve your problem!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文