In the next few lessons we will learn how communication occurs between two systems. There are a variety of methods, one of the most predominant is REST.
REST is an acronym for REpresentational State Tranfer. It's designed to network applications that use HTTP methods for communication.
HTTP is an acronym for Hypertext Transfer Protocol. It's a protocol for transferring data over the internet.
Specific applications that often use REST are web services and API's.
API is an acronym for Application Programming Interface. It's a set of rules and protocols used to communicate for a specific application between different systems.
Why don't we take a look at what this terminology means in context? Discord is an application that Digi Cafe uses for its community. It also has an API that is built on top of HTTP/REST - discord API reference. Say a developer wants to build an application that interacts with Discord in various ways, perhaps by posting an automated message in a channel when something occurs to trigger it. This will be considered a POST response. This is because the developer is 'posting' or 'putting' something to the server. The converse is a GET response. An example of this might be the developer getting statistics on number of active users in a community. This is because the developer is 'getting' something from the server.
Why don't we practice now? We will be communicating with httpbin, an open source HTTP request and response service.
To begin with, we need to install a new module. Here is a line of code that should be entered in a terminal to do so.
conda install -c conda-forge urllib3We start by importing a module urllib3. Python has various
libraries available to it for REST, alternatives
are requests and httpx. We've chosen this as
it is a bit lower level while still being relatively straight forward to
use.
The first thing this library requires us to do is to set up
a PoolManager. This object handles connection pooling and thread
safety on your behalf. This object then has various methods we can use,
one of which is used to make requests - requests. This
method has two required arguments and multiple optional
arguments. requests(METHOD, URL), where METHOD is
one of GET or POST. URL is the url of the target
system. Putting these together we can then make a request. What is
returned is an object that contains a response.
The first print statement shows the status of the response. The second
print statement shows the body of the response. The body is returned
in binary. To convert it from binary to string, we may use the method
decode().
We're not done with terminology just yet. When we recieve a response, we often want a simple status as to whether the message was received in good standing, if there was something wrong with the message format, or whether the server was even reached. You can see a list of codes here. A few to get us started: 200 indicates a successful request, 400 indicates a bad message, i.e. the header is incorrect or the body wasn't formatted correctly, 404 indicates the url is not recognized (so the message knew how to target the server using the base url, but the extended part of it, the server didn't recognize). We will cover more aspects of the response as well as urls headers in the next three lessons.
Why don't we look at a POST request now. As mentioned before,
this request is asking to put some data to the server. As such, we need
to include data. Depending on the library used, the argument might be
slightly different as well as what formatting might occur to the data in
this field. With urllib3, one argument we can use is termed
body, this will take a json as argument. To convert a
dictionary to json, we use the method json.dumps for this
conversion. To convert binary back to string, we again use
decode().
Good work so far! Why don't you try it yourself? Please make a GET request to the url https://httpbin.org/base64/Q2FmZmUgaXMgbG92ZQ==. Then print the response's data as a string.