nodejs API 基本身份验证的证书链错误中的自签名证书
self signed certificate in certificate chain error for nodejs API Basic authentication
我正在尝试使用 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:\Users\Desktop\test.js:10:26
at Request.handleRequestResponse [as _callback] (C:\Users\node_modules\unirest\index.js:444:15)
at self.callback (C:\Users\Bittu.Raju\node_modules\request\request.js:185:22)
at Request.emit (events.js:315:20)
at Request.onRequestError (C:\Users\Bittu.Raju\node_modules\request\request.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");
并尝试在 header 中使用 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) {
...
另一种选择是使用 this other solution
指定自签名 CA 证书
我正在尝试使用 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:\Users\Desktop\test.js:10:26
at Request.handleRequestResponse [as _callback] (C:\Users\node_modules\unirest\index.js:444:15)
at self.callback (C:\Users\Bittu.Raju\node_modules\request\request.js:185:22)
at Request.emit (events.js:315:20)
at Request.onRequestError (C:\Users\Bittu.Raju\node_modules\request\request.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");
并尝试在 header 中使用 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) {
...
另一种选择是使用 this other solution
指定自签名 CA 证书