nodejs API基本身份验证证书链中的自签名证书错误



我正在尝试使用REST API将数据添加到我的弹性搜索索引中,它在POSTMAN中运行良好。我还用一个python脚本尝试了同样的方法,请求正在运行。

但nodejs同样失败了。它在证书链中给我自己签名的证书错误。

var unirest = require('unirest');
console.log("start");
var req = unirest('PUT', 'https://24.73.543.228:9001/accelerators/_doc/12')
.headers({
'Content-Type': 'application/json',
'Authorization': 'Basic ZWxhc3RpYzpQYXNzd29yZEAxcjdcMjM0='
})
.send(JSON.stringify({"name":"ajj","type":"test","description":"khukhkd","created_date":"2021-02-18T20:20:41.560Z"}))
.end(function (res) { 
if (res.error) throw new Error(res.error); 
console.log(res.raw_body);
});

这就是错误。

Error: Error: self signed certificate in certificate chain
at C:UsersDesktoptest.js:10:26
at Request.handleRequestResponse [as _callback] (C:Usersnode_modulesunirestindex.js:444:15)
at self.callback (C:UsersBittu.Rajunode_modulesrequestrequest.js:185:22)
at Request.emit (events.js:315:20)
at Request.onRequestError (C:UsersBittu.Rajunode_modulesrequestrequest.js:877:8)
at ClientRequest.emit (events.js:315:20)
at TLSSocket.socketErrorListener (_http_client.js:426:9)
at TLSSocket.emit (events.js:315:20)
at emitErrorNT (internal/streams/destroy.js:92:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)

尝试了基本身份验证

auth = "Basic " + new Buffer(username + ":" + password).toString("base64");

并尝试在标头中使用auth变量,但仍然存在相同的错误。

对Python请求进行了同样的尝试,效果很好。如何使其在nodejs中工作。

import requests
from requests.auth import HTTPBasicAuth
import json
url="https://24.73.543.228:9001/accelerators/_doc/12"
print(url)
payload=dataJson[i]
print(payload)
payload=json.dumps(payload)
print(type(payload))
headers = {
'Content-Type': 'application/json',
}
response = requests.request("PUT", url, headers=headers, data=payload,auth=HTTPBasicAuth('usern', 'Password@123'),verify=False)
print(response.text)

由于您的证书是自签名的,您可以通过调用strictSSL(false):来尝试不验证它

var req = unirest('PUT', 'https://24.73.543.228:9001/accelerators/_doc/12')
.strictSSL(false)       <---- add this line
.headers({
'Content-Type': 'application/json',
'Authorization': 'Basic ZWxhc3RpYzpQYXNzd29yZEAxcjdcMjM0='
})
.send(JSON.stringify({"name":"ajj","type":"test","description":"khukhkd","created_date":"2021-02-18T20:20:41.560Z"}))
.end(function (res) { 
...

另一个选项是使用此其他解决方案指定自签名CA证书

相关内容

  • 没有找到相关文章

最新更新