从 LocalHost NodeJS/Express 应用程序向 API 的请求失败,但在 PostMan 中工作?

Requests to API from LocalHost NodeJS/Express app fail, but work in PostMan?

这是我现在使用的代码,并计划在我可以从他们的 API 请求返回令牌后成为中间件。

exports.requestToken = function (req, res) {
  const connectionInfo = {
    params: {
      whitelabel_id: crowdContentAuth.api_username,
      whitelabel_key: crowdContentAuth.api_password,
      client_id: crowdContentAuth.client_id,
      client_key: crowdContentAuth.client_key,
    },
  };
  const options = {
    hostname: "api.crowdcontent.com/?request=" + JSON.stringify(connectionInfo),
    port: 443,
    method: "POST",
    headers: {
      "Content-Type": "application/json",
    },
  };
  http.request(options, (res) => {
    console.log(res);
  });
};

当我从我的应用程序请求时,我在控制台中看到以下内容:

0|Server |   errno: 'ENOTFOUND',
0|Server |   code: 'ENOTFOUND',
0|Server |   syscall: 'getaddrinfo',
0|Server |   hostname: 'api.crowdcontent.com/?request={"params":{"whitelabel_id":"redacted","whitelabel_key":"redacted","client_id":"redacted","client_key":"redacted"}}'

我正在编辑响应中的内容,显然是因为它不是 public 数据,但它就在那里。 CrowdContent API 也有点旧,并且不完全是您通常期望基于 POST 的 API 工作的方式,因为数据应该在请求正文中传递,而不是通过 URL.

API 声明使用 https://DOMAIN/request= 但是在 PostMan 中测试后它似乎是 https://api.crowdcontent.com/?request={stringifiedObjectOfDetails - 当我通过 [=37 这样发送请求时=]伙计,我确实得到了带有令牌的成功响应。

{"message":"Token is already created. Add the token to your API call are you are good to go.","token":"5713a4d4100b2redacting41e76b4efef0","last_access":"blah"}

这是我的请求做错了吗,或者这可能是 CloudFlare 参与了请求,因为它来自我的 localhost/ip?

此时无法弄清楚是什么原因造成的。

我的应用程序中的所有其他 API 和 Post 请求均有效。

我认为你应该使用其他包,如 axios、node-fetch,而不是使用 http 内置模块。