GET request succeeds in POSTMAN and Chrome but fails with axios

Viewed 504

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

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!

Related