[英]Got npm module: How to add bearer token to POST request
我最近發現request
不再被維護,所以我找到的最好的替代方法是got
。 我正在嘗試對 REST 服務器進行外部 API 調用,但我不確定如何在 POST 請求的授權標頭中添加不記名令牌。
這是我的代碼:
const response = await got.post(
"https://${SERVER}/${SOME_ID}/conversations/${CONVERSATION_ID}/messages",
{
json: [
{
text: req.body.message,
type: "SystemMessage",
}
],
responseType: "json",
headers: {
token: "Bearer pXw4BpO95OOsZiDQS7mQvOjs"
}
}
);
這會導致401 Unauthorized
。 我無法在 GOT 提供的文檔中找到此類實現的方向。 而且由於關於這個包的查詢並不多,我也沒有在谷歌上找到任何東西。 如果有人可以在這方面幫助我,那將非常有幫助!
您確定標題名稱是 "token" 嗎? 通常在 API 中,Bearer 位於名為“Authorization”的標頭中
const response = await got.post(
"https://${SERVER}/${SOME_ID}/conversations/${CONVERSATION_ID}/messages",
{
json: [
{
text: req.body.message,
type: "SystemMessage",
}
],
responseType: "json",
headers: {
"Authorization": "Bearer pXw4BpO95OOsZiDQS7mQvOjs"
}
}
);
這是代碼
npm i postman-request
npm包的鏈接
const request = require('postman-request');
request({
url: 'your url',
headers: {
'Authorization': 'Bearer 71D50F9987529'
},
rejectUnauthorized: false
}, function(err, res) {
if(err) {
console.error(err);
} else {
console.log(res.body);
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.