0
我試圖訪問一個API,我需要傳遞的API密鑰和API的祕密,但我在節點JS做它。 在Python中,你可以這樣做:節點JS中python中auth的等價物是什麼?
requests.get('https://api.github.com/user', auth=('user', 'pass'));
我的問題是我如何在節點JS做到這一點?我是否將密鑰和祕密包含在標題中或將其包含在選項對象中?
這是代碼:
var options = {
host:'linktowebsite',
path:'/data',
headers: {
' Content-Type': 'application/x-www-form-urlencoded'
}
};
var req = http.request(options, function(res) {
console.log(`STATUS: ${res.statusCode}`);
console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
res.setEncoding('utf8');
res.on('data', (chunk) => {
console.log(`BODY: ${chunk}`);
});
res.on('end',() => {
console.log('No more data in response.');
});
});
req.on('error', (e) => {
console.log(`problem with request: ${e.message}`);
});
使用要求,它的工作@Matt – bkk