Mark C.
Mark C.

Reputation: 1915

cURL works but axios.get doesn't

This seems a popular genre of question but I haven't been able to find the answer elsewhere. I have a Node.JS script as follows:

const axios_1 = require("axios");                                                                    
const dataUrl = "https://maps.amtrak.com/services/MapDataService/trains/getTrainsData";              
console.log("getting");                                                                              
axios_1.get(dataUrl).then(                                                                           
  (data) => {console.log(data);}                                                                     
)                                                                                                    
.catch((err) => {console.log(err);});                                                                

I get a 403 on the axios GET above, but if I do curl https://maps.amtrak.com/services/MapDataService/trains/getTrainsData I get data. Any ideas on what I should look for to fix the problem?

Upvotes: 2

Views: 2512

Answers (1)

Phil
Phil

Reputation: 164760

Seems to work in Node v18 (but not v16) using the following...

axios.get(dataUrl, {
  headers: {
    accept: '*/*',
    'user-agent': 'curl/7.79.1',
  },
  timeout: 5000, // it has a habit of timing out
});

This at least uses the exact same headers as curl and it seems to work reasonably consistently.

I haven't bothered to set up a Wireshark SSL proxy to really inspect the differences but might do over the weekend because this is somewhat fascinating.

Upvotes: 3

Related Questions