API Requests using Python
What is an API?
API stands for Application Programming Interface. It is commonly known as API. It provides an environment that helps two or more computer programs to contact each other. Basically, it is a kind of interface that provides assistance to other pieces of software. An API specification is a standard or document that helps in understanding the making and use of this type of interface or connection.
A computer system can only expose or implement an API if it meets this document or standard. It is like a User Interface, but rather than connecting humans and computers, it is built to connect fragments of software to each other.
It is designed in such a manner that it cannot be used by a person other than the programmer that is integrating it into the software. Normally, an API is made up of multiple parts. These parts can be accessed by the programmers. These parts act as services or tools, and the programmers use them according to requirements, the part being used by the programmer is said to be called by him. The usage and implementation of these calls are defined by an API specification.
One of the multiple motives of an API is to hide the interior details of the working of the system, it only exposes those parts that are important and are to be used by the programmers. It also keeps systems consistent, though later the internal details may change.
The Purpose of an API
The main purpose of an API in building different applications is that it clarifies programming by extracting the basic execution and it only exposes the necessary objects required by the programmer. Like a graphical interface for an email user provides a button to perform certain tasks regarding emails, and an API build to perform input/output operations in a file provides a user with the function to copy the file from one location to another, this process does not require the user to understand the operations taking place in the backend of the application.
History of APIs
APIs first came into existence on February 7th, 2000. It was introduced by Salesforce.com. Before that, initially, the term API described an interface only for application programs. In 1990, its definition was updated to “a set of services that can be accessed by the programmer to perform specific tasks”. This definition was given by technologist Carl Malamud.
Uses of API
APIs are used in different fields, some of which are described below:
- Frameworks and libraries: APIs are used for building the interface to software libraries. Since an API defines and directs “the expected behaviour” of the application whereas the library is the genuine implementation of those set of rules.
A single API can be implemented in multiple ways of different library models that share a common programming interface. - Operating Systems: An API can easily describe the interface between the operating system and an application. For example, POSIX provides a set of similar API descriptions that solely aims to authorize an application written for the operating system, conformant with POSIX, to be executed in another operating system with the same preference (i.e., conformant with POSIX).
Berkeley Software Distribution and Linux are the operating systems that are able to implement POSIX APIs. - Remote APIs: These APIs allow programmers to operate faraway resources using protocols. These protocols are the specific standard used for communication, this helps in making different technologies work together independent of their platform or language.
- Web APIs: These APIs are the services accessed through the user’s devices (such as mobile phones, PCs, laptops, etc.) to a server using the HTTP (Hypertext Transfer Protocol). The user’s device sends requests in the form of HTTP requests and these requests get a response message in JSON (JavaScript Object Notation) or XML (Extensible Mark-up Language) format.
A few examples of APIs currently available:
- EHLLAPI
- Java APIs
- Cocoa and Carbon for Macintosh
- DirectX for Microsoft Windows
- OpenGL cross-platform graphics API
- Simple DirectMedia Layer (SDL)
- Server Application Programming Interface (SAPI)
Advantages of using an API
- These help programmers in grasping the skills of other applications.
- While developing new applications, an organization or a developer need not recreate codes for processes like authentication, payment processing, communication, etc.
- With the help of APIs, the system components and applications can easily communicate with each other internally as well as over the internet too.
Disadvantages of using an API
- If an API is traded-off, the rest of all systems and applications are at risk.
- Since, one can access an API over the internet. It too has vulnerabilities as other internet-based resources carry with them.
- The APIs are unsafe against CSRF attacks, man-in-middle attacks, SQL injection, DDoS attacks, and XSS attacks.
Using Python to make API requests
If we want to work with APIs in Python, there are certain tools that will be required to make those requests. The request library is the most common library in Python to work with APIs and make requests. This library does not come along with the standard Python library, so in order to use it, we will have to install it first.
To install the requests library, thebelow-given command has to be executed in the command prompt:
pip install requests
Our first API Request
Requests can be made using different methods. The most common of them are:
- GET request method
- POST request method
Since, we will be focussing only on retrieving data, so, we will use only the “GET request method.”
The GET request method
The GET Request Method simply redeems particulars from the server. It is used to request data. The user can pass additional data as a part of this request method to define the query more clearly.
We use requests.get() function to make a “GET” request. It only needs an argument; this argument is the URL we are going to make a request.
Example:
#Importing the requests library
import requests as rq
request = rq.get('http://www.javatpoint.com/')
# Requesting page encoding from the server
e = request.encoding
print("Encoding: ",e)
# Requesting response code from the server
s = request.status_code
print("Response code: ",s)
# Requesting response time from the server
t = request.elapsed
print("Response Time: ",t)
# Requesting header from the server
hdr = request.headers['Content-Type']
print("Header: ", hdr)
# Requesting text from the server
rqt = request.text
print("A small portion of text from the web page:", rqt[0:500])
Output:
