When I run your exact code, I get an error in the error parameter. That's why both response and body are empty. You have an error. The specific error is this:
Error: options.uri is a required argument
at Request.init (D:\code\test\temp\node_modules\request\request.js:231:31)
at new Request (D:\code\test\temp\node_modules\request\request.js:127:8)
at request (D:\code\test\temp\node_modules\request\index.js:53:10)
at Object.<anonymous> (D:\code\test\temp\temp.js:3:1)
at Module._compile (internal/modules/cjs/loader.js:956:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)
at Module.load (internal/modules/cjs/loader.js:815:32)
at Function.Module._load (internal/modules/cjs/loader.js:727:14)
at Function.Module.runMain (internal/modules/cjs/loader.js:1047:10)
at internal/main/run_main_module.js:17:11
Always check for errors before you try to use the other parameters and log any errors and it will save you a lot of time.
If you change to this, it works for me:
const request = require('request');
request({
method: 'GET',
uri: 'https://api.publicapis.org/entries?category=animals&https=true',
}, function (error, response, body){
if (error) {
console.log(error);
return;
}
const data = response.body;
const apiData = JSON.parse(data)
console.log('Returned: ', apiData)
if(response.statusCode == 200){
console.log('success');
}
else{
console.log("error with api call")
}
});
Several things to note:
The request() library has been deprecated. While it will be maintained for a while (perhaps a long while), it will not be enhanced with new features any more. There's a list of alternatives that are still being actively developed here. I'm using got() because it seems very nice and simple and quick to use and it entirely promise-based.
Always check for errors and log them before you try to use any of the other arguments. That will save you a ton of debugging time.
When sending with res.json(), you should pass it a Javascript object, not already converted JSON. Since you already have that with data, that's what I changed your call to.
server failed to parse request: schema: invalid path "htt"&httfrom the end because api.publicapis.org/entries?category=animals (withouthtt) returns JSON data