Reputation: 109
Struggling to get nodeJS https.request or https.get to work with the imgur API (also tried with http module). Here is my code for a https.request:
var https = require('https')
var imgurAPIOptions = {
hostname : 'api.imgur.com',
path: '/3/gallery/search/time/1/?q=cat',
headers: {'Authorization': 'Client-ID xxxxxxxxxxxx'},
json: true,
method: 'GET'
};
https.request(imgurAPIOptions,function(err,imgurResponse){
if (err) {console.log('ERROR IN IMGUR API ACCESS')
} else {
console.log('ACCESSED IMGUR API');
}
});
It returns the error message console.log.
Here is the (working) code for an equivalent client side request using jQuery AJAX:
$(document).ready(function(){
$.ajax({
headers: {
"Authorization": 'Client-ID xxxxxxxxxxxx'
},
url: 'https://api.imgur.com/3/gallery/search/time/1/?q=cat',
success:function(data){
console.log(data)
}
})
});
Has anyone here had any experience in getting the imgur API working? What am I missing?
Upvotes: 2
Views: 2515
Reputation: 1984
Take a look at the https docs. You need to make a few changes:
The first paramater in the request callback is the reponse, rather than an error. If you want to check for errors, you can listen for the error
event on the request.
Once the request has recieved data then you can output it.
var https = require('https');
var options = {
hostname: 'api.imgur.com',
path: '/3/gallery/search/time/1/?q=cat',
headers: {'Authorization': 'Client-ID xxxxxxxxxxxx'},
method: 'GET'
};
var req = https.request(options, function(res) {
console.log('statusCode:', res.statusCode);
console.log('headers:', res.headers);
res.on('data', function(d) {
process.stdout.write(d);
});
});
req.on('error', function(e) {
console.error(e);
});
req.end();
Upvotes: 1