關于這個主題還有其他問題,但似乎沒有什么對我有用。
我有一個功能 CURL,但我想翻譯成 JS(使用 Node)。
卷曲
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", algorithm=MD5, qop="auth", stale=false content-type: application/json內容長度:106 x-envoy-upstream-service-time:2 日期:2022 年 2 月 16 日星期三 17:05:37 GMT 服務器:特使
HTTP/2 200 日期:2022 年 2 月 16 日星期三 17:05:37 GMT 嚴格傳輸安全性:max-age=31536000;包括子域;referrer-policy: strict-origin-when-cross-origin x-permitted-cross-domain-policies: none x-content-type-options: nosniff x-mongodb-service-version: gitHash=8284a4ea05955cb13c38df5489a3794b9a691d4f; versionString=v20220216 內容型別:應用程式/json x-frame-options:拒絕內容長度:216 x-envoy-upstream-service-time:154 服務器:envoy
我不確定為什么會有 401 和 200,但它確實有效。但是我得到一個沒有--digest標志的 401,下面的解決方案似乎都沒有包含摘要?
以下是我嘗試過的所有方法,均回傳 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) => {
...
});
節點庫卷曲
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, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "...");
curl_setopt($ch, CURLOPT_USERPWD, 'user:pass');
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
$result = curl_exec($ch);
curl_close($ch);
編輯包request-digest
digestRequest
.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);
});
回復
錯誤:錯誤請求,答案為空
uj5u.com熱心網友回復:
PHP
您需要指定它是一個摘要:
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
節點獲取
這是我在這里找到的:
fetch(url, {
...
headers: {
'Authorization': 'Basic ' Buffer.from(`${username}:${password}`, 'binary').toString('base64')
}
...
})
將您的代碼與上面建議的代碼進行比較,我發現您的代碼, 'binary'中缺少該代碼。
請求摘要
有一個請求摘要包可以幫助你。示例(取自本段中共享的鏈接):
打回來
var digestRequest = require('request-digest')('username', 'password');
digestRequest.request({
host: 'http://test.com',
path: '/api/v1/test.json',
port: 80,
method: 'GET',
headers: {
'Custom-Header': 'OneValue',
'Other-Custom-Header': 'OtherValue'
}
}, function (error, response, body) {
if (error) {
throw error;
}
console.log(body);
});
僅承諾
var digestRequest = require('request-digest')('username', 'password');
digestRequest.requestAsync({
host: 'http://test.com',
path: '/api/v1/test.json',
port: 80,
method: 'GET',
excludePort: false,
headers: {
'Custom-Header': 'OneValue',
'Other-Custom-Header': 'OtherValue'
}
})
.then(function (response) {
console.log(response.body);
})
.catch(function (error) {
console.log(error.statusCode);
console.log(error.body);
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/429376.html
標籤:javascript 卷曲
上一篇:澄清陣列和指標的關系
