Web Services Mini-exercise: What's in a URL?
Concepts taught: - Anatomy of a URL - The ? delimiter - name=value pairs - character substitution in URL strings: e.g. space --> + - URL "hacking" - The REST architecture
In many ways the World Wide Web is made up of thousands of publicly accessible web services. We use them every day. One of the most common is a simple Google search.
Open a web browser to google.com. Enter a topic to search and press return or click the search button to execute the search. Say you searched on "French revolution". You'll see the accustomed search results, of course. But look at the long string of characters that appears in the address field. It begins with http://www.google.com/ followed by a delimiter (normally a ?
), followed by a series of what are called name=value pairs. This may be quite long; e.g.:
http://www.google.com/?q=french+revolution&oq=French+re&aqs=chrome.0.0j69i57j0l6.3444j0j7&sourceid=chrome&ie=UTF-8
These name=value pairs are separated from one another by a & character. The pairs represent the bits of information that get sent to Google's web search service.
Look carefully through these name=value pairs. Eventually you'll encounter one that looks like this:
…&q=French+revolution&…
As you can see, this is the actual query that you typed in the search field. You can modify this search string directly in the address field by simply replacing the search text, taking care to use a + sign in place of each space, e.g.:
…&q=south+america+economy&…
Try this now. Modify this URL string and press return. Note that the search results are based on the modified search string.
The point here is twofold:
URL strings of the type we just looked at are typical ways to access web services using the simple and widely used REST architecture. This is the type of web service we want to explore, so as you look for web services to experiment with, look for REST as the type of service.
Now try this in LiveCode: 1. Create a card with these controls: - field "urlfld" - field "contents" - button "search" 2. Copy/paste the URL from above into fld "urlfld" 3. In button "search" enter the following script on mouseUp put URL fld "urlfld" into tRawHTML set the htmlText of fld "contents" to tRawHTML end if Click the button, and you will see that LiveCode is able to partially render the HTML that google.com returns. But LiveCode also has a secret trick--the ability to render a full-blown web browser in a stack. Do this: 1. Drag a *browser widget* from the tools palette. It has a world globe icon. 2. Resize it to cover most of the card. 3. Now change the button's script to this: on mouseUp set the url of widget "browser" to fld "urlfld" end mouseUp 4. Now click the button. The entire Google search results should be perfectly and fully rendered in the browser widget.