[英]npm request with headers and auth
我正在嘗試使用“請求”npm訪問API。 此API需要標頭“content-type”和基本身份驗證。 這是我到目前為止所做的。
var request = require('request');
var options = {
url: 'https://XXX/index.php?/api/V2/get_case/2',
headers: {
'content-type': 'application/json'
},
};
request.get(options, function(error, response, body){
console.log(body);
}
).auth("dummyemail@abc.com","password",false);
在使用Node執行此操作時,我收到一條錯誤,指出無效的用戶名和密碼。 我使用下面的命令使用CURL驗證了相同的API,身份驗證和標頭,它給出了預期的HTTP響應。
curl -X GET -H“content-type:application / json”-u dummyemail@abc.com:password" https://XXX/index.php?/ api / V2 / get_case / 2 “
請建議使用auth和header編碼請求的正確方法。
這是我的更新代碼
var auth = new Buffer("dummy@gmail.com" + ':' + "password").toString('base64');
var req = {
host: 'https://URL',
path: 'index.php?/api/v2/get_case/2',
method: 'GET',
headers: {
Authorization: 'Basic ' + auth,
'Content-Type': 'application/json'
}
};
request(req,callback);
function callback(error, response, body) {
console.log(body);
}
我在控制台中看到“未定義”。 你能在這幫我嗎?
這是它對我有用的方式
var auth = new Buffer(user + ':' + pass).toString('base64');
var req = {
host: 'https://XXX/index.php?/api/V2/get_case/2',
path: path,
method: 'GET',
headers: {
Authorization: 'Basic ' + auth,
'Content-Type': 'application/json'
}
};
request(url, {
method: "POST",
auth: {
user: this.user,
pass: this.pass
}
}, function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log('body:', body);
} else {
console.log('error', error, response && response.statusCode);
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.