Examples on how to use the "Send web request" and "Get image from" bricks

Last modified by admin on 2020/09/12 04:35

For important general, and especially safety related information, please see https://catrob.at/webbricks.

These bricks can be extremely dangerous, so read the information provided on this page carefully. Really!!

The "Send web request" and "Get image from" bricks allow you to access the internet in order to get some information from, e.g., wikipedia, including images from the web. Images can be directly used as looks of an object, and this can for instance be used to retrieve parts of real world maps if a web service allows to do so. Please note that many of these services require API keys, which can be either free of charge but require registration, or commercial and cost money. Make sure to read the warnings on https://catrob.at/webbricks for dangers associated with the use of these bricks. 

To take full advantage of the capabilities of the web bricks, you can use the "join" function to build more complex web addresses required for accessing relevant content, as well as the "regular expression" function to extract relevant information from text your project downloaded with the "Send web request" brick.

For a comprehensive list of the capabilities of regular expressions, please consult the official documentation for the Android version of Catrobat's apps: https://developer.android.com/reference/java/util/regex/Pattern

Further documentation and help regarding advanced topics is available here.

Explanations on how to get API keys if necessary for some of the web services listed below are usually available via the address of the service. Some searching may be required.

NOTE the very important safety considerations when using API keys explained on this page.

Trusted domains:
Explanations and examples:
TitleAddressDescriptionSample Catrobat projectsAPI key
Random jokeshttps://official-joke-api.appspot.com/random_joke

The free joke api returns data in the following json format: {"id":10,"type":"general","setup":"What kind of shoes does a thief
wear?","punchline":"Sneakers"}

To extract the setup phrase in case you stored the result in variable "joke", use the following regular expression:
regular expression( '"setup":"(.*?)"' , "joke" )
The result will be: What kind of shoes does a thief wear?
To extract the setup phrase, use the following regular expression:
regular expression( '"punchline":"(.*?)"' , "joke" )

Elephant that tells random jokes when tappedNo
Geocoding and geoparsinghttps://geocode.xyz/

The free worldwide geocoding and geoparsing API, with a query such as
join( 'https://geocode.xyz/' , join( latitude , join ( ',' , join(
longitude , '?geoit=json' ) ) ) ) )

returns data in the following json format when you execute the Catrobat project with above statement when your are in London, UK:
{   "statename" : {},   "distance" : "0.000",   "elevation" : "20",
  "state" : "UK",   "latt" : "51.50354",   "city" : "LONDON",   "prov" :
"UK",   "geocode" : "LONDON-MCRXA",   "geonumber" : "3154700960970",
  "country" : "United Kingdom",   "stnumber" : "10",   "staddress" :
"DOWNING STREET",   "inlatt" : "51.50354",   "alt" : {      "loc" : {
        "staddress" : "DOWNING STREET",         "stnumber" : "10",
        "postal" : "SW1A 2AA",         "latt" : "51.50354",         "city"
: "LONDON",         "prov" : "UK",         "longt" : "-0.12768",
        "class" : {}      }   },   "timezone" : "Europe/London",
  "region" : "Greater London, England",   "postal" : "SW1A 2AA",   "longt"
: "-0.12768",   "remaining_credits" : {},   "confidence" : "1",
  "inlongt" : "-0.12768",   "class" : {},   "altgeocode" :
"SYNERGY-MCRXA"}

For example, to extract the elevation in meters above sea level in case you stored the result in variable "geographic location", use the following regular expression:
regular expression( '"elevation" : "(.*?)"' , "geographic location" )
The result will be: 20
For other possible usages, see https://geocode.xyz/api

TODO: Some project that does something with geography (NOTE: this link must be corrected to a relevant example once the brick is included in the release)No
"hello" in various languageshttps://fourtonfish.com/hellosalut/hello/

This free service returns a json text with the word "hello" in a chosen language.
Example: https://fourtonfish.com/hellosalut/?cc=de where de stands for German (= deutsch in German), returns
{"code":"de","hello":"Hallo"}
To extract the German phrase in case you stored the result in variable "hello in json format", use the following regular expression:
regular expression( '"hello":"(.*?)"' , "hello in json format" ) )
The result will be: Hallo 

TODO: Some project that does something with the hello service (NOTE: this link must be corrected to a relevant example once the brick is included in the release)No
Extract pure text from HTML pageWorks with any plain HTML web page. The example uses Wikipedia.This project shows how to get the readable text part from a web page by eliminating html tags when using a "Send web request" brick. The demo uses the first descriptive paragraph from English Wikipedia pages.

Test cases are also available when setting the corresponding flag in the first object.
https://share.catrob.at/app/project/3fbad7bf-f17f-11ea-9251-005056a36f47