startWebRequest

Category:Data

startWebRequest(url, callback)

Category: Data

Requests data from the internet and executes code when the request is complete.

Your apps are not limited to data they collect and generate. Many web sites make data available for your apps to access. startWebRequest() should be used with the URL of web services that are designed to be called in that way.

The callback function will be passed three parameters:

  • The status of the request (completed or failed), using HTTP status codes.
  • The type of data returned by the service (text, image etc.).
  • The data itself in JSON format.

Examples


Example: Print the name and population of every country

startWebRequest("https://restcountries.eu/rest/v2/all?fields=name;population", function(status, type, content) {
  var jsonContent = JSON.parse(content)
  for(var i = 0; i< jsonContent.length; i++){
    console.log(jsonContent[i]);
  }  
});

Example: This simple example lets users type in a number and returns a random fun fact about the number.

textInput("numberInput", "");
setProperty("numberInput","placeholder","Type a number");
textLabel("factOutput", "");
button("newInput", "Get fact!");

onEvent("numberInput", "change", function() {
  var number = getNumber("numberInput");
  var url = "http://numbersapi.com/" + number;


  startWebRequest(url, function(status, type, content) {
    if(status == 200) {
      setText("factOutput", content);
    }
  });
});

Syntax

startWebRequest(url, function(status, type, content) {
  Code to execute
});

Parameters

Name Type Required? Description
url string Yes The web address of a service that returns data.
callback function Yes A function that is asynchronously called when the call to startWebRequest() is finished. Three paramters are passed to this function.

Returns

When startWebRequest() is finished executing, the callback function is automatically called.

Tips

  • startWebRequest() has a callback because it is accessing an external web service and therefore will not finish immediately.
  • The callback function can be inline, or separately defined in your app and called from startWebRequest().
  • Do not put functions inside a loop that contain asynchronous code, like startWebRequest(). The loop will not wait for the callback function to complete.
  • In order to write code that reads the content of the response returned by the service, we need to know the format of the response. This will often be documented online by the service, in the above case Yahoo Weather.
  • You will also need to use the JSON.parse function, which can be used to transform the string of JSON into an object that we can use in our code.

Allowed hostnames

For security reasons, only URLs with certain hostnames can be accessed using startWebRequest. Currently, the hostname must end in one of the following:

  • accuweather.com
  • apex.oracle.com
  • api.coinmarketcap.com
  • api.data.gov
  • api.datamuse.com
  • api.energidataservice.dk
  • api.exchangeratesapi.io
  • api.football-data.org
  • api.foursquare.com
  • api.nasa.gov
  • api.open-notify.org
  • api.openweathermap.org
  • api.pegelalarm.at
  • api.randomuser.me
  • api.rebrandly.com
  • api.spacexdata.com
  • api.spotify.com
  • api.themoviedb.org
  • api.thingspeak.com
  • api.zippopotam.us
  • atlas.media.mit.edu
  • bible-api.com
  • code.org
  • compete.hsctf.com
  • data.austintexas.gov
  • data.cityofchicago.org
  • data.gv.at
  • data.nasa.gov
  • developers.zomato.com
  • donordrive.com
  • dweet.io
  • enclout.com
  • githubusercontent.com
  • googleapis.com
  • hamlin.myschoolapp.com
  • herokuapp.com
  • hubblesite.org
  • images-api.nasa.gov
  • isenseproject.org
  • lakeside-cs.org
  • opentdb.com
  • pokeapi.co
  • qrng.anu.edu.au
  • quandl.com
  • quizlet.com
  • rejseplanen.dk
  • maker.ifttt.com
  • noaa.gov
  • numbersapi.com
  • random.org
  • restcountries.eu
  • rhcloud.com
  • runescape.com
  • samples.openweathermap.org
  • sheets.googleapis.com
  • spreadsheets.google.com
  • stats.minecraftservers.org
  • swapi.co
  • transitchicago.com
  • translate.yandex.net
  • vpic.nhtsa.dot.gov
  • wikipedia.org
  • words.bighugelabs.com
  • pastebin.com

Want to use a URL that's not currently allowed? Let us know at support@code.org.

Found a bug in the documentation? Let us know at documentation@code.org

Found a bug in the documentation? Let us know at documentation@code.org