繁体   English   中英

HTTPS请求在Node.js中不起作用(代码400)

[英]HTTPS request is not working in Node.js (Code 400)

我是Node.js的新手,并尝试使用基本身份验证和标头/正文调用现有的API。 以下是有关我尝试使用的API的更多详细信息。 我总是收到400码作为答复。 请检查我随附的代码段:

 // Include the request library for Node.js var request = require('request'); // Basic Authentication credentials var username = 'myEmail'; var password = 'mySuperSecret'; var authenticationHeader = "Basic " + new Buffer(username + ":" + password).toString("base64"); var json = '{"device":"","os_type":"Android","os_version":"4.0","dvc_manuf":"unknown","dvc_type":"unknown"}'; var jsonstring = JSON.stringify(json); const options = { method: 'POST', uri: 'https://api.indego.iot.bosch-si.com/api/v1/authenticate', headers: { 'Authorization': 'Basic ' + new Buffer(username + ':' + password).toString('base64'), 'Content-Type': 'application/json', 'Content-Length': jsonstring.length }, body : jsonstring } request(options, function(err, res, body) { if (!err && res.statusCode == 200) { let jsonbody = JSON.parse(body); console.log(jsonbody); console.log(res); console.log(res.statusCode); } }); 

我还尝试了一个Web客户端来测试API,并成功完成了: https : //client.restlet.com/ 在此处输入图片说明

有什么建议我做错了吗? 谢谢,tro

我建议您进行一些更改,

在您的options变量的下面一行中, url应更改为uri

uri: 'https://api.indego.iot.bosch-si.com/api/v1/authenticate',

此外,仅在呼叫成功时将响应放置在显示器上,

  if (!error && response.statusCode == 200) {
    let jsonbody = JSON.parse(body);
    console.log(jsonbody);
    console.log(res);
    console.log(res.statusCode);
  }

希望这可以帮助!

我尝试了以下方法,现在可以使用:

 var request = require('request-promise'); var options = { method: 'POST', uri: 'https://api.indego.iot.bosch-si.com/api/v1/authenticate', headers: { 'Authorization': 'Basic myBasicAuth', 'Content-Type': 'application/json', 'Content-Length': '116' }, body: { "device":"", "os_type":"Android", "os_version":"4.0", "dvc_manuf":"unknown", "dvc_type":"unknown" }, json: true // Automatically stringifies the body to JSON }; request(options, function (err, response, body) { var reason; if (err) { reason = { cause: err, error: err, options: options, response: response }; } else if (!(/^2/.test('' + response.statusCode))) { // Status Codes other than 2xx reason = { statusCode: response.statusCode, error: body, options: options, response: response }; } }); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM