我需要在标头参数中使用Ocp-Apim-Subscription-Key调用外部API

时间:2019-08-20 10:32:08

标签: node.js

我已经测试了使用邮递员对api的调用,并且效果很好。我需要从我的ALexa lambda函数进行https调用。我在下面的代码进行各种tweek的请求中传递标题信息时遇到麻烦。当不提供标头参数时,我可以获得401状态代码,因此可以点击url。任何帮助将不胜感激。谢谢

 const getRemoteData = function (url) {
 return new Promise((resolve, reject) => {
 const client = url.startsWith('https') ? require('https') : 
 require('http');
 let headers = new HttpHeaders({'Ocp-Apim-Subscription-Key':'xxxxxxxx'});
 const request = client.get(url, {headers}, (response) => {
  if (response.statusCode < 200 || response.statusCode > 299) {
    reject(new Error('Failed with system status code: ' + 
 response.statusCode));
  }
  const body = [];
  response.on('data', (chunk) => body.push(chunk));
  response.on('end', () => resolve(body.join('')));
  });
 request.on('error', (err) => reject(err))
})
};

0 个答案:

没有答案