Reputation: 113
I'm trying to write an app using Node.js and Express.js (I'm working through the Free Code Camp curriculum, if that's useful information) that takes in a search term and sends and inquiry to the Imgur API and report back some data about images related to the search term. I've registered my application and received the necessary Client-ID and have included a header per the documentation here and am getting a statusCode of 200 from Imgur, but instead of any "data", I'm getting a number of other properties in the returned object that are not useful. What do I need to do to get image data from Imgur based on a search term?
The URL I'm sending the request to:
https://api.imgur.com/3/gallery/search/{search term}
per the documentation here: https://api.imgur.com/endpoints/gallery#gallery-search. The response is not in the format described
Search function:
this.askImgur = function(req, res) {
var img_data;
console.log('askImgur function firing');
var search_term = url.parse(req.originalUrl||req.path).query;
console.log(search_term);
var search_path = path + search_term;//PLUS PAGE
var options = {
protocol: "https:",
host:'api.imgur.com',
path:search_path,
method:'GET',
headers: {
"Authorization":"Client-ID <CLIENT ID HERE>"
}
};
var ds;
https.get(options, function(res) {
console.log("Got response: " + res.statusCode);
for (var key in res) {
if (res.hasOwnProperty(key)) {
console.log(key);
}
}
}).on('data', function(chunk){
ds+=chunk;
console.log("chunk is "+chunk);//does nothing
}).on('error', function(e) {
console.log("Got error: " + e.message);
});
console.log("ds is "+ds);//does nothing
//res.json("askImgur function is sending you words");
res.send(search_term);
};//askImgur function
The output:
Got response: 200
_readableState
readable
domain
_events
_eventsCount
_maxListeners
socket
connection
httpVersionMajor
httpVersionMinor
httpVersion
complete
headers
rawHeaders
trailers
rawTrailers
upgrade
url
method
statusCode
statusMessage
client
_consuming
_dumped
req
Based on the documentation, the image data should be in a "data" property, is not returned with the above properties.
Upvotes: 0
Views: 2657
Reputation:
private OkHttpClient httpClient = new OkHttpClient.Builder().build();
Request request = new Request.Builder()
.url("https://api.imgur.com/3/gallery/hot/viral/0.json")
.method("GET", null)
.addHeader("Authorization", "Client-ID <<client_id>>")
.build();
Replace << client_id >> with your 15 character Client ID.
Eg: ".addHeader(Authorization", "Client-ID a1b2c3d4e5f6g7h")
Upvotes: 0
Reputation: 880
try using node-fetch
, I personally find the API of it a lot easier to use. So go ahead and npm i node-fetch
.
Then try the code below:
const fetch = require("node-fetch")
const term = "lolcat"
const url = `https://api.imgur.com/3/gallery/search/top/1/?q=${term}`
const IMGUR_API_CLIENT = "111111" // your client api
fetch(url, {headers: {Authorization: `Client-ID ${IMGUR_API_CLIENT}`}})
.then(res => res.json())
.then(json => console.log(json))
I hope that helps. Sorry if it's too late.
Upvotes: 0
Reputation: 2435
Please try this url,
https://api.imgur.com/3/gallery/search?q={search term}
Upvotes: 2