Axios GET请求无效

时间:2017-11-22 14:29:56

标签: javascript node.js http get axios

我正在使用: Axios:0.17.1 节点:8.0.0

以下标准Node get工作正常,但Axios版本没有。有什么想法吗?

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

我只是抓到了一个503,"请求失败,状态代码为503"

4 个答案:

答案 0 :(得分:3)

您似乎可以将代理详细信息传递给Axios FYI。

来自文档...

  // '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'
    }
  },

答案 1 :(得分:0)

唯一对我有用的是取消设置代理:

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

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

答案 2 :(得分:0)

我想你可能忘了回复axios的反应。

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

注意在axios.get之前和buildResponse之前返回

答案 3 :(得分:0)

withCredentials中使用request config属性,可以解决您的问题。

 axios
    .get(`${someUrl}`, { withCredentials: true })
    .then(function(response) {
        return buildResponse(response).then(results => res.send(results));
    })
    .catch(function(error) {
        handleError(error, res);
    });
相关问题