5

I'm using: Axios: 0.17.1 Node: 8.0.0

The following standard Node get works fine, but the Axios version does not. Any ideas why?

Node http:

    http
    .get(`${someUrl}`, response => {
        buildResponse(response).then(results => res.send(results));
    })
    .on('error', e => {
        console.error(`Got error: ${e.message}`);
    });

Axios:

axios
    .get(`${someUrl}`)
    .then(function(response) {
        buildResponse(response).then(results => res.send(results));
    })
    .catch(function(error) {
        handleError(error, res);
    });

I just get a 503 in the catch, with "Request failed with status code 503"

Ben Taliadoros
  • 7,003
  • 15
  • 60
  • 97
  • 1
    what happens if you go to the url in your browser? – curv Nov 22 '17 at 14:35
  • Data comes back fine, just like the http.get() – Ben Taliadoros Nov 22 '17 at 14:35
  • Can you try it in postman or curl? Also comment out this line and try it again: buildResponse(response).then(results => res.send(results)); – curv Nov 22 '17 at 14:41
  • Interestingly curl returns html which renders: The following error was encountered while trying to retrieve the URL: *theurl* Connection to 127.0.0.1 failed. The system returned: (111) Connection refused The remote host or network may be down. Please try the request again. Your cache administrator is root. – Ben Taliadoros Nov 22 '17 at 15:02
  • I have the neccasasry certs in chrome, which are needed, and I get the same issue in FF which lascks the certs, but why does the http.get work? – Ben Taliadoros Nov 22 '17 at 15:06
  • I was going to say, is there any auth stuff going on... I'm not too sure about this one without seeing your environment etc, wierd... Are you sure there is no config in your app (that you need to pass as a header in the Axios request for example) – curv Nov 22 '17 at 15:11
  • Seems to be a proxy issue, when I unset it and curl it works, not sure why unsetting it would make it work – Ben Taliadoros Nov 22 '17 at 15:15
  • 1
    Ahh I love those ones! You would have to dig into Axios source to see what is going on – curv Nov 22 '17 at 15:19
  • I guess the native node process honours some environment variable set for the proxy – curv Nov 22 '17 at 15:25

4 Answers4

3

It seems that you can pass Proxy details to Axios FYI.

From the docs...

  // 'proxy' defines the hostname and port of the proxy server
  // Use `false` to disable proxies, ignoring environment variables.
  // `auth` indicates that HTTP Basic auth should be used to connect to the proxy, and
  // supplies credentials.
  // This will set an `Proxy-Authorization` header, overwriting any existing
  // `Proxy-Authorization` custom headers you have set using `headers`.
  proxy: {
    host: '127.0.0.1',
    port: 9000,
    auth: {
      username: 'mikeymike',
      password: 'rapunz3l'
    }
  },
curv
  • 3,796
  • 4
  • 33
  • 48
3

The only thing that worked for me was unsetting the proxy:

delete process.env['http_proxy'];
delete process.env['HTTP_PROXY'];
delete process.env['https_proxy'];
delete process.env['HTTPS_PROXY'];

From: Socket hang up when using axios.get, but not when using https.get

Ben Taliadoros
  • 7,003
  • 15
  • 60
  • 97
  • Yea that did work, I couldn't make the axios proxy work, could've been an error on my end, but this works so I'm going for it for now! – Ben Taliadoros Nov 22 '17 at 15:28
  • 1
    brilliant answer! I was deploying on a system and wasn't even aware that a http_proxy was set. Cost me two days. Finally your answer gave the solution. – Chris Jun 23 '20 at 11:40
0

I think you might forgot to return axios response.

return axios
    .get(`${someUrl}`)
    .then(function(response) {
        return buildResponse(response).then(results => res.send(results));
    })
    .catch(function(error) {
        handleError(error, res);
    });

Notice return before axios.get and before buildResponse

Amit Kumar Khare
  • 565
  • 6
  • 17
0

use withCredentials property in your request config which will resolve your issue.

 axios
    .get(`${someUrl}`, { withCredentials: true })
    .then(function(response) {
        return buildResponse(response).then(results => res.send(results));
    })
    .catch(function(error) {
        handleError(error, res);
    });
Roopak Puthenveettil
  • 1,387
  • 2
  • 13
  • 27