×

Python Rest API

In this tutorial, we will understand the meaning of API and REST API. We will understand the working of REST API. We will then realize the boundaries of architecture defined by REST. Further, we will understand different methods for CRUD operation.

API

It is a set of guidelines that allows programs to interact with each other. API is created by developers on a server and permits the client to interrelate with it.

Rest API

The Representational State Transfer is the expanded name of REST. It takes HTTP requests into account for communication with web services. It uses numerous values such as HTTP, JSON, URL, and XML for data communication and transfer. It is a set of protocols that developers keep in mind while dealing with the creation of API.

Working

A client sends a request in the form of a web URL in the form of HTTP GET or HTTP POST or HTTP PUT or HTTP DELETE request. Further, an answer is received from the server in the form of resources which may be either HTML, XML, Image, or JSON.  JSON is the most commonly used resource among all.

Boundaries on Architecture as defined by the REST:

  • Client-server Architecture: The Client and Server architecture is necessary for a REST application. The client is responsible for requesting resources and the server is responsible for storing the resources. Neither of them depends on each other for anything. They function independently.
  • Cacheable: It is important for every response to include whether the reply is cacheable or not and the time for which the response can be cacheable on the client-side. The client will direct the data from its cache for any following request without the need of sending the data again to the server.  
  • Standard Interface: This interface enables the client to interact with the server in a single language that is consistent and standardized irrespective of the architectural assistance.
  • Stateless: It is the request which is capable of storing the important states to handle the requests. While the server stores nothing as such.
  • Layered System: An application architecture comprises several layers. Each layer is independent of the others but the intermediate layer. The intermediate layer plays a key role in improving the readiness of the system.
  • Code on Demand: It is a discretionary feature. Here, the server is responsible for providing executable code to the client.

Method

There is five CRUD (Create, Read, Update and Delete) methods popularly used in REST based architecture:

GET: The GET method is employed to read or retrieve an illustration of a resource. It returns an illustration in the format XML or JSON.

POST : The POST verb is most often employed to create new resources. To be precise, It is used to create subsidiary resources.

NOTE: It is easy for the users to use GET and POST methods without the need to install any methods override.

PUT: It is employed to update the aptitudes. It is preferred when the user chooses to update or replace all the existing resources in a go.  Though, PUT can also be used to create a resource in the case where the resource ID is chosen by the client instead of by the server.

DELETE: It is employed to delete a resource identified by a URI. The unique key of the object is passed to delete a key.

NOTE: To use PUT and DELETE methods, the user needs to install methods override which can be done in the following way.

npm install method-override –save

The following package needs to be installed before being able to use the above.

var methodOverride = require("method-override");

PATCH: It is mainly used to update a request on the server just like PUT. But the difference is it can be employed to delete patches or part of data and not the whole data in a go. The PATCH request only requires to comprise the changes to the resource, not the complete resource.

Now, let us understand endpoints with the aid of a table.

URI	HTTP 	Explanation
api/users	GET	Employed to fetch all the users
api/users	POST	Employed to add a user
api/users/2	PUT	Employed to update a user with id = 2
api/users/2/edit	GET	It shows the edited form of the user with id = 2
api/users/2	DELETE	Employed to delete a user with id = 2
api/users/2	GET	Get a user with id = 2

NOTE: To keep an API URI (Universal Resource Identifier) consistent, it is needed to use plurals in the URL.


Related Topics

How To Install Python In Ubuntu

How To Install Python In Ubuntu Ubuntu is free and open-source software and it is an essential part of the Linux distribution. It is a popular operating system developed by Canonical. If we...

3 minutes read.

How to run Python program in CMD

How to run python program in cmd Command Prompt provides us all together with a different approach for dealing with our programs. The programs can be executed by accessing the directories. In...

3 minutes read.

Python Multithreading

In python, we can implement multithreading. In this tutorial, we will understand the basics of implementing multithreading in a python programming language. Multithreading is just like multiprocessing. We use it...

4 minutes read.

Speech Recognition in Python

What is Speech Recognition? Speech Recognition is a term defined for automatic recognition of human speech. Speech recognition is the most significant activity in the domain of the interaction between the...

8 minutes read.

How to add 2 lists in Python?

In Python, a list is defined as a data structure that contains a sequence of elements. It can contain any kind of data type inside it but in order to concatenate two...

3 minutes read.

Python Stack

Python Stack: The work Stack is defined as arranging a pile of objects or items on top of another. It is the same method of allocating memory in the stack...

10 minutes read.

Python program to check whether a given number is prime or not

Python program to check whether a given number is prime or not A positive integer greater than 1 is called a prime number if it is divisible by one and number...

1 minute read.

Python - Binomial Distribution

Introduction Definition of the Binomial Distribution The method of counting how many instances of a specific event there have been is called the binomial distribution. It will outline the possible outcomes or...

4 minutes read.

Import Module in Python

In this article, you will learn everything about “ Import ” in Python. Modules in python that are already created can be accessed and used in another code by importing the...

6 minutes read.

Multiprocessing in python

The word multiprocessing is used to compute the operation in which more than two processors run simultaneously with more than one central processor. The computer's performance increases gradually. Multiprocessing is...

6 minutes read.

Python Time Module

Python contains many files that can be imported into a python code and used whenever we want. One of that modules is the time module. It is a good practice...

6 minutes read.

Python program to print array element present at even position

Python program to print array element present at even position In this program, we'll see a Python program that prints the elements of an array that are in even positions. We...

1 minute read.

Comments in the Python Programming Language

In this tutorial, we will learn how to comment in multiple lines and paragraphs in the python programming language. Commenting a Paragraph in Python Most people think the importance of comments is...

2 minutes read.

PyDev with Python IDE

What is IDE? Integrated Development Environment is the abbreviation of IDE. It is a coding tool that automates code editing, finding errors and testing. IDE combines all the developer tools into...

3 minutes read.

Python Num2words

Python: Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It is said...

3 minutes read.

SKLearn Model Selection

The model selections of SK learn has many functions with which we can work on.It has functions to cross-validate the model and, it also provides validation and learning curves.It is...

3 minutes read.

Python Program to Generate a Random String

The term "random" refers to a group of information or data that can be accessed in any chronological order. To create random strings, a Python program called random is utilized....

6 minutes read.

Python ltrim() function

Python ltrim() function The ltrim() function in PHP removes whitespace or other predefined characters from the beginning or left side of a string. The related functions are as follows: rtrim() – This function is used to...

1 minute read.

Standard Scaler in SKLearn

The sci kit learns in python is a library thatch is used in machine learning which is used to work on data modeling.It is only focused on the data modeling,...

4 minutes read.

Python tuple() function

Python tuple() function The tuple() function in Python creates a tuple object. Syntax tuple([iterable]) Parameter Iterable: This parameter represents a sequence, collection or an iterator object. Return This function returns a tuple object. Example 1 # Python Program explaining #...

1 minute read.