【问题标题】:Sending a GET HTTPS request with basic auth to an API向 API 发送带有基本身份验证的 GET HTTPS 请求
【发布时间】:2020-08-25 06:56:45
【问题描述】:

我正在尝试向用户/通过受保护的 API 发送 GET 请求。我正在使用 Javascript 并添加了“身份验证”选项。

const https = require('https');

const headers= {
    'Accepts' : 'application/json',
}

const options = {
  hostname: 'api.com/more/info',
  port: 443,
  path: '/',
  auth: 'username:password',
  method: 'GET'
};

const req = https.request(options, (res) => {
  console.log('statusCode:', res.statusCode);
  console.log('headers:', res.headers);

  res.on('data', (d) => {
    process.stdout.write(d);
  });
});

req.on('error', (e) => {
  console.error(e);
});
req.end();

运行此代码时我收到的错误是:

Error: getaddrinfo ENOTFOUND api/com/more/info
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:66:26) {
  errno: -3008,
  code: 'ENOTFOUND',
  syscall: 'getaddrinfo',
  hostname: 'api.com/more/info'
}

主机名已更改为匿名。

我做错了什么?

提前感谢您的任何建议。

【问题讨论】:

  • 您是否尝试过使用 Postman 发送请求?只是为了确保您的计算机指向的DNS服务器可以解析api.com/more/info

标签: javascript node.js api https get


【解决方案1】:

'api.com/more/info' 可能不是您的主机名。

请尝试以下选项:

const options = {
  hostname: 'api.com',
  port: 443,
  path: '/more/info',
  auth: 'username:password',
  method: 'GET'
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-21
    • 2019-05-05
    • 2019-08-08
    • 2017-07-23
    • 2014-02-01
    • 2019-10-26
    • 1970-01-01
    相关资源
    最近更新 更多