External API Call

Hi guys,
i started a small NodeJS project, with Express here on Glitch.
My api needs to call an external api for gathering results.
Is this possible from a Glitch project? I tried it but the call fails.
Thanks guys!

Yes - Glitch is essentially a server with Nodejs pre-installed. You can use a npm module like request to make requests.

Yep, i just installed the request dependency but the call to the external api is not working. Is there any policy to enable or something like that?

Just another detail. The call is not working if nested in a request GET block. This is the code snippet:

app.get("/ping", function(request, response) { 
  request.get(myEndPoint, (error, response, body) => {
        if(error) {
            return console.dir(error);
        }
        console.log(JSON.parse(body));
    });
});

Do you mind sharing the external API endpoint?

Yeeep. Is a public API: http://ergast.com/api/f1/current/last/results.json.
As said, the call is not working when nested in a app.get block. If i put this call just at the start of the script, the data are downloaded.
This problem is only on Glitch, on my local machine the problem is not present.

It seems to work fine for me on Glitch. I’m using the following code

app.get("/cool", (req, res) => {
  request.get("http://ergast.com/api/f1/current/last/results.json", (error, response, body) => {
    if (error) {
      console.dir(error);
      res.send("err");
    }
    console.log(JSON.parse(body));
    res.send("ok");  
   });
});

The issue of your code is that when you’re trying to use the request library. However, the function(request, response) overwrites the request variable.

2 Likes

Damn, what a dumb error…too much food during this holidays :smiley: Thank you so much :smiley:

1 Like