There has been other questions on the subject, but nothing seems working for me.
I have a functional curl , but I want to translate to JS (with Node).
curl
curl --user "uername:pass" --digest \
--header "Content-Type: application/json" \
--include \
--request POST "https://cloud.mongodb.com/api/atlas/v1.0/groups/MY_GROUP/clusters/MY_CLUSTER/fts/indexes?pretty=true" \
--data '{
"collectionName": "collname",
"database": "myDB",
"mappings": {
"dynamic": true
},
"name": "default"
}'
响应
HTTP/2 401 www-Authenticate:Digest Realm="MMS Public API", domain="",NONCE="OtwBmcu89QuyVMLW6FP1W/ZjD3preQl0",算法rithm=MD5, QoP="auth",stale=false content-type:application/json Content-Length: 106 x-特使-upstream -服务时间:2日期:星期三,2022年2月16日17:05:37 GMT服务器:特使
HTTP/2 200日期:星期三,2022年2月16日17:05:37格林尼治标准时间 严格传输安全:max-age=31536000;包含子域; 推荐人-策略:跨地域时严格原产地 x-允许跨域策略:无x-内容类型-选项: nosniff x-mongoDB-service-version: gitHash=8284a4ea05955cb13c38df5489a3794b9a691d4f; versionString=v20220216内容类型:application/json X-Frame-Options:拒绝内容-长度:216 X-特使-upstream -服务-时间:154服务器:特使
我不知道为什么会有401和200,但它可以用.然而,我得到的是没有--digest
标志的401,下面的解决方案似乎都没有包含摘要?
下面是我try 过的所有方法,它们都返回401"您无权使用此资源":
获取
fetch(url,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization:
'Basic ' +
Buffer.from(
'user:pass'
).toString('base64'),
},
body: dataString,
}
)
与以下各项的结果相同
请求
const headers = {
'Content-Type': 'application/json',
};
var options = {
url: url,
method: 'POST',
headers: headers,
body: dataString,
auth: {
user: 'user',
pass: 'pass',
},
};
request(options, (error: any, response: any, body: any) => {
...
});
node-libcurl
const curl = new Curl();
curl.setOpt(Curl.option.HTTPHEADER, [
'Content-Type: application/json',
]);
curl.setOpt(Curl.option.URL, url);
curl.setOpt(Curl.option.POST, true);
curl.setOpt(Curl.option.USERPWD, 'user:pass');
curl.setOpt(Curl.option.POSTFIELDS, dataString);
甚至
PHP
$ch = curl_init();
curl_setopt($ch, curl OPT_URL, $url);
curl_setopt($ch, curl OPT_RETURNTRANSFER, 1);
curl_setopt($ch, curl OPT_POST, 1);
curl_setopt($ch, curl OPT_POSTFIELDS, "...");
curl_setopt($ch, curl OPT_USERPWD, 'user:pass');
curl_setopt($ch, curl OPT_HTTPHEADER, ['Content-Type: application/json']);
$result = curl_exec($ch);
curl_close($ch);
Edit for the package 100
digest请求
.requestAsync({
host: url,
path: urlPath,
port: 80,
method: 'POST',
json: true,
headers: {
'Content-Type': 'application/json',
},
body: body,
})
.then(function (response: any) {
console.log(response);
})
.catch(function (error: any) {
console.log(error);
});
响应
错误:请求错误,答案为空