【问题标题】:Request working from Postman but resetting from Axios in the command line从 Postman 请求工作,但在命令行中从 Axios 重置
【发布时间】:2018-07-02 15:24:05
【问题描述】:

我遇到了一个奇怪的问题。我正在测试一个 API,并在 Postman 中勾勒出我的请求。事实上,他们目前在那里工作得很好。

POST /authEndpoint
content-type: application/json
accept: application/json
cache-control: no-cache
postman-token: some-token
user-agent: PostmanRuntime/7.1.1
host: greatapi.com
accept-encoding: gzip, deflate
content-length: 68
{
 "user": "user",
 "password": "secret"
}

我得到了我想要的回应。现在我尝试在 JavaScript 中使用 Axios 重新创建这个请求。问题是,我得到的不是响应,而是读取 ECONNRESET。我相信我就像在 Postman 中一样勾勒出它,所以这很奇怪。这是我的 Axios 配置:

import AXIOS from 'axios';

const BASE_URL = 'https://greatapi.com';

const axios = AXIOS.create({
  baseURL: BASE_URL,
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json',
    'Cache-Control': 'no-cache',
  }
});

axios.post('/authEndpoint', {
  'user': 'user',
  'password': 'secret',
})
  .then(response => console.log(response.data))
  .catch(error => console.error(error.message));

具体回应是这样的:

read ECONNRESET

你有什么想法吗?

【问题讨论】:

    标签: javascript http postman axios


    【解决方案1】:

    原来是公司防火墙的问题。我可以通过使用 https-proxy-agent 包并将 Axios 上的代理设置为它,传递我的公司代理来修复它。

    const axios = AXIOS.create({
      ...
      httpsAgent: new HttpsProxyAgent('http://127.0.0.1:3128'),
      proxy: false,
      ...
    });
    

    【讨论】:

      猜你喜欢
      • 2017-11-19
      • 2021-07-14
      • 1970-01-01
      • 2011-05-11
      • 2021-07-20
      • 2020-01-23
      • 1970-01-01
      • 2014-12-07
      • 1970-01-01
      相关资源
      最近更新 更多