REST
We will now try to perform http requests in Python, using the requests
module.
We will perform requests on these Web services:
Some terminologies before we begin:
API
A Web API (Application Programming Interface) enable servers to define how clients should communicate with them programmatically.
Clients can usually access some resources from a server via an endpoint. For example:
- https://api.github.com/users/josiahwang returns a JSON with some info about the Github user named josiahwang.
- https://api.github.com/users/josiahwang/repos returns a JSON with a list of josiahwang’s Github repos.
Note the subtle difference that unlike a web server that simply serves an HTML file (e.g. http://www.josiahwang.com/index.htm), these endpoints might actually not be folders or files at all. They might for example just point to a function to be executed on the server side.
REST
In case you are wondering what REST means (don’t we all need a rest now?), it stands for REpresentational State Transfer.
REST is a software architectural style for Web services that follows certain principles. It defines how protocols like HTTP should be used. Any Web API that follows these guiding principles are called RESTful API.
We won’t discuss REST in much detail. It is enough to know that RESTful APIs are subject to some constraints:
- Client-server architecture: Clients and servers are separate
- Statelessness
- Each request should be independent of previous requests
- So each request must contain all of the information necessary to understand the request
- Cacheability: Resources should allow caching unless explicitly indicated otherwise
- Layered system
- Uniform interface
(There is no need to fully understand these for this module).
APIs that do not strictly conform to these constraints are known as REST-like APIs.
REST API responses tend to be in JSON (most common), text or XML (but can be anything really). This allows both the servers and clients to be written in whatever programming language the authors prefer. And I assume you all love Python the most! 😊