[英]get data asynchron from Rest api and loading them to mongodb with node.js - using promises
[英]How to get data from REST api using an API key in node js?
我在我的節點服務器中使用了第 3 方 REST API。 第 3 方 API 提供者給了我一個 API 密鑰和 cURL 中的一個示例,如下所示:
$ curl -u APIKey@https://xyzsite.com/api/v1/users
我不確定如何在 node js 中做到這一點。 我試過跟隨但沒有運氣。 我得到
var options = {
host: "xyzsite.com",
path: "/api/v1/users",
headers: {
"Authorization": "Basic " + myAPIKey
}
};
https.get(options, function(res, error) {
var body = "";
res.on('data', function(data) {
body += data;
});
res.on('end', function() {
console.log(body);
});
res.on('error', function(e) {
console.log(e.message);
});
});
控制台消息
{
"message": "No authentication credentials provided."
}
像這樣改變你的要求..
https.get(myAPIKey + "@https://xyzsite.com/api/v1/users",function(res,error){
var body = "";
res.on('data', function(data) {
body += data;
});
res.on('end', function() {
console.log(body);
});
res.on('error', function(e) {
console.log(e.message);
});
});
或者,如果您想使用options
對象..
var options={
host:"xyzsite.com",
path:"/api/v1/users",
auth: myAPIKey
};
https.get(options,function(res,error){
var body = "";
res.on('data', function(data) {
body += data;
});
res.on('end', function() {
console.log(body);
});
res.on('error', function(e) {
console.log(e.message);
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.