最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Axios GET request not working - Stack Overflow

programmeradmin4浏览0评论

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"

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"

Share Improve this question asked Nov 22, 2017 at 14:29 Ben TaliadorosBen Taliadoros 9,41118 gold badges66 silver badges105 bronze badges 9
  • 1 what happens if you go to the url in your browser? – curv Commented Nov 22, 2017 at 14:35
  • Data es back fine, just like the http.get() – Ben Taliadoros Commented Nov 22, 2017 at 14:35
  • Can you try it in postman or curl? Also ment out this line and try it again: buildResponse(response).then(results => res.send(results)); – curv Commented Nov 22, 2017 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 Commented Nov 22, 2017 at 15:02
  • 1 Ahh I love those ones! You would have to dig into Axios source to see what is going on – curv Commented Nov 22, 2017 at 15:19
 |  Show 4 more ments

4 Answers 4

Reset to default 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'
    }
  },

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

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

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);
    });
发布评论

评论列表(0)

  1. 暂无评论