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