I'm having trouble getting "startWebRequest" to work with an API

127 Views Asked by At

Currently, I'm trying to run this code in Applab on code.org:

var url = "https://api.apilayer.com/exchangerates_data/symbols?to=";
var request = {
    method: "GET",
    headers: {
        "apikey": "XOIlrz9JbDcWlmjNvG9vuBwUDRByRpEt",
        "Content-Type": "application/json"
    }
};

startWebRequest(url, request, function(status, json) {
    if (status === 200) {
        console.log(json.symbols);
    } else {
        console.error("Error: " + status);
    }
});

The error I get in the console says "startWebRequest() callback parameter value ([object Object]) is not a function."

Would anyone know the solution to this problem?

I tried using the "fetch("https://api.apilayer.com/exchangerates_data/symbols?to=", requestOptions)" element but it seems code.org is too hold to have "fetch" pre defined. Instead, it uses this thing called "startWebRequest" and I was unsure how it works so I told ChatGPT to convert my code from "fetch" to "startWebRequest" and this is what it gave me.

1

There are 1 best solutions below

0
Cengiz On

startWebRequest() takes two parameters; URL and a callback function. You need to give those parameters in order and can't give other parameters like request. It was expecting a function but you give a request object which is why it gives an error.

But the main problem is, code.org has a list of allowed hostnames for us to make an API call and api.apilayer.com is not one of them. So you can't make an API call from api.apilayer.com using App Lab.

Here you can check the allowed hostnames; https://studio.code.org/docs/ide/applab/expressions/startWebRequest