Before you keep reading...
Runestone Academy can only continue if we get support from individuals like you. As a student you are well aware of the high cost of textbooks. Our mission is to provide great books to you for free, but we ask that you consider a $10 donation, more if you can or less if $10 is a burden.
Before you keep reading...
Making great stuff takes time and $$. If you appreciate the book you are reading now and want to keep quality materials free for other students please consider a donation to Runestone Academy. We ask that you consider a $10 donation, but if you can give more thats great, if $10 is too much for your budget we would be happy with whatever you can afford as a show of support.
7.2. Interacting with Other Services¶
The key to interacting with other services is to have an efficient way to exchange data. Using the HTTP protocol this data must be plain text. One way to think about this is in terms of calling a remote function. The name of the function is the URI. But how do you pass parameters to this remote function? There are two ways that a browser can send data to a server.
As part of the URL known as a QUERY_STRING
In the body of an HTTP request.
7.2.1. Query String Parameters¶
The query string is used to send data from the browser to the server when an HTTP GET request is made. As the name suggests it is a special string that is appended to the end of the URL. You have probably seen many query strings while using the web. Here is an example:
In the example above, the query string is everything after the ?. the string is composed of key value pairs separated by the & character. In the example above we have the following key value pairs
Notice that the syntax is very regular to make it easy for a program to take the string and split it into the key value pairs. In the early days of web programming the query string was provided to the web application in the form of an environment variable. The program had to split the string into key value pairs on its own. In modern web frameworks the query string is automatically processed and provided to the program in the form of a dictionary.
188.8.131.52. Check your Understanding¶
Write a python function that given a URL like http://example.com/api/getdata?id=1234&date=now&apikey=1234567 returns a dictionary containing the correct keys and values.
What if you want to pass more complicated data to the web api? What if you have more data to pass to the api than you can do as part of the URL? In that case you can send the data in the body of the http request.
Since we need to adhere to the HTTP protocol any large data or any complex data objec needs to be turned into a string before it can be sent as part of the body. The process of turning an object from a program into a string is known as serialization. Web programming uses two main formats to serialize data: XML and JSON. JSON is highly convenient and is much more widely used today than XML, so we will focus on JSON.