【问题标题】:Consuming get API in nodejs with authentication using https在 nodejs 中使用 get API 并使用 https 进行身份验证
【发布时间】:2020-06-07 11:33:39
【问题描述】:

如何将Id and password 表示basic authentication 发送到以下代码。我的意思是我需要在下面的代码中放置 id/pass 的位置?我的 API 需要基本身份验证

const https = require('https');

https.get('https://rws322s213.infosys.com/AIAMFG/rest/v1/describe', (resp) => {
  let data = '';
  resp.on('data', (chunk) => {
    data += chunk;
  });


  resp.on('end', () => {
    console.log(JSON.parse(data).explanation);
  });

}).on("error", (err) => {
  console.log("Error: " + err.message);
});

【问题讨论】:

    标签: node.js rest http-headers http-authentication


    【解决方案1】:

    为了请求Basic authentication,客户端将http Authorization header 传递给服务器。该标题采用以下形式

        Authorization: Basic Base64EncodedCredentials
    

    因此,您的问题是“如何通过 https.get() 请求传递标头?”

    It goes in options.headers{} 你可以把它放在那里:

    const encodedCredentials = /* whatever your API requires */ 
    const options = {
      headers: {
        'Authorization' : 'Basic ' + encodedCredentials
      }
    }
    
    const getUrl = https://rws322s213.infosys.com/AIAMFG/rest/v1/describe'
    https.get (getUrl, options, (resp) => {
       /* handle the response here. */
    })
    

    【讨论】:

      猜你喜欢
      • 2020-02-28
      • 2014-06-04
      • 1970-01-01
      • 2012-04-23
      • 2017-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多