1

Im using express with node.js & having trouble consuming data from this free api https://api.publicapis.org/entries?category=animals&https=true any feedback will be helpful!

This code outputs the following error message TypeError: Cannot read property 'body' of undefined

 request({
    method: 'GET',
    host: 'https://api.publicapis.org',
    path:  '/entries?category=animals&https=true',
 }, function (error, response, body){
    const data = response.body;
    const apiData = JSON.parse(data)
    console.log('Returned: ', apiData)
    if(!error && response.statusCode == 200){
      res.json(body);
    }
    else{
      console.log("error with api call")
    }
 })
5
  • response is null, therefore body is not there. may i ask why there is htt is your host path? if you delete it you get a response as far as i can see Commented May 12, 2020 at 23:32
  • Posting the URL in the browser (not node.js) gives me this result: server failed to parse request: schema: invalid path "htt" Commented May 12, 2020 at 23:38
  • My guess is all you need to do is remove &htt from the end because api.publicapis.org/entries?category=animals (without htt) returns JSON data Commented May 12, 2020 at 23:39
  • @MücahidErenler My bad, I didnt copy the path correctly to this question. This is the url api.publicapis.org/entries?category=animals&https=true and with the correct url I still get a 'Cannot read property 'body' of undefined' when running the code Commented May 13, 2020 at 2:49
  • @slebetman I didnt copy the url correctly to this question, my mistake. The host I'm using in my code is 'api.publicapis.org' and path '/entries?category=animals&https=true' Commented May 13, 2020 at 2:51

1 Answer 1

1

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:

  1. 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.

  2. 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.

  3. 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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your feedback! I ran the changes you made & I'm getting the following error 'getaddrinfo ENOTFOUND api.publicapis.org'
@CatGirl19 - I don't know what to say. When I run this code exactly as it shows here as a stand-alone program, it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.