In this article, we are going to introduce the concepts of API and RESTful API and also check how to communicate with a web service in Python using the Requests module.
API stands for Application Programming Interface, which can generally be said to be an interface and a common boundary between two things. For example, User Interface or abbreviated UI refers to the common boundary between a program or an operating system and a real user (the user in the DOS operating system dealt with a type of user interface that was similar to text, but in Windows it works with windows.) Now, with the same definition, API refers to the common boundary between two applications; In other words, when we want to design an application, the way the application interacts with other applications will depend on the API.
Familiarity with Web API and RESTful API concepts
Web API is actually an API that is implemented in the form of web applications and by using it, it is possible to allow other programs to connect to the mentioned API with a series of specific parameters and get an output from it in a specific format, which is one of the most important ones. We can refer to the API of social networks Facebook, Twitter, Instagram, Telegram, etc.
In response to the question, what are the advantages of using web APIs? It can be said that the most important reason is that the extracted data is real-time and updated, and the next reason is the lack of storing large data because you only call the data you need.
RESTful, derived from the word Representational State Transfer, is actually one of the types of API design architecture that has become very popular in recent years, so that in the RESTful architecture, the client, similar to a browser, sends its request in the form of the HTTP protocol and with methods that are understandable for the browser, such as It sends GET, POST, PUT, DELETE, etc. to the intended server and receives a response, which is generally in the form of JSON or XML (in contrast to RESTful, a new standard API called GraphQL has been released, for more information, you can visit Refer to the article How to GraphQL: a comprehensive self-study to learn GraphQL.)
Using the Requests module in Python
In Python, in order to communicate with an API or web service, we use the Requests module to send HTTP requests, but at the same time, other modules such as Urllib, which are built-in in the Python programming language, can also be used. Working with them is not as simple as the Requests module. To work with the Requests module, we must first install it as follows:
$ pip install requests
To ensure the installation of this module, we import it in the console as follows:
import requests
If there is no error, it means that the Requests module is installed correctly. Now let’s assume that we want to send a GET request to a desired link and check the response:
import requests
req = requests.get(‘https://github.com/timeline.json’)
The GET request has been sent to the GitHub timeline page (as if we had opened the mentioned link in the browser) and we have saved the answer in a variable called req, and now we can get the information we want using different methods. For example, we will have:
req.text
u'{"message":"Hello there, wayfaring stranger. If you\u2019re reading this then you probably didn\u2019t see our blog post a couple of years back announcing that this API would go away: http://git.io/17AROg Fear not, you should be able to get what you need from the shiny new Events API instead.","documentation_url":"https://developer.github.com/v3/activity/events/#list-public-events"}
We can view the content of the page using the text method, or with:
req.json()
{u'documentation_url': u'https://developer.github.com/v3/activity/events/#list-public-events', u'message': u'Hello there, wayfaring stranger. If you\u2019re reading this then you probably didn\u2019t see our blog post a couple of years back announcing that this API would go away: http://git.io/17AROg Fear not, you should be able to get what you need from the shiny new Events API instead.'}
All information on the page can be converted to Jason or using the encoding method as follows:
req.encoding
'utf-8'
You can see the encoding format of the page. The Requests module in Python also includes POST and PUT methods, which POST is used to create a new object and PUT is used to edit and update objects that have already been created. For example, we have:
r = requests.post(‘http://127.0.0.1/api/v1/add_item’, data = {‘task’:‘Shopping’})
r = requests.put('http://127.0.0.1/api/v1/add_item', data = {'task':'Shopping at 2'})
And finally, it should be said that Requests includes various other features such as Authentication methods, which are among the more professional applications of this module.