×

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

Cube Root in Python

In general, the cube is a three-dimensional solid figure that has 6 square faces. It is also called a geometrical shape with six equal faces, eight vertices, and twelve edges....

3 minutes read.

Python String lstrip() method

Python String lstrip() method The string. lstrip () method in Python returns a copy of the string with leading characters removed (based on the string argument passed). Syntax string.lstrip([chars]) Parameter chars(optional): This parameter represents a...

1 minute read.

Python list() | List class in Python

The list() class in Python creates a list object where the list object is a collection which is ordered and changeable. Syntax class list([iterable]) Parameter Iterable: It is a required parameter which represents a sequence, collection...

1 minute read.

Python Web Development projects

What is Python? Python is currently one of the most widely used computer programming languages. This isn't just an exaggeration; Python is the second-most famous programming language on the software development...

3 minutes read.

Handling missing keys in Python dictionaries

In this lesson, you will discover how to create a Python application that will manage missing keys in a dictionary. Python dictionaries store data as key-value pairs, where each value...

3 minutes read.

Python Dictionary copy() method

Python Dictionary copy() method The dictionary.copy () method in Python returns a copy of the specified dictionary. Syntax dictionary.copy () Parameter NA Return None Example 1 # Python program explaining # the dictionary.copy() method # initialising the dictionary ...

1 minute read.

Python program to find whether a given number is even or odd

Python program to find whether a given number is even or odd A number is said to be even if any number is completely divisible by 2, which means there is...

1 minute read.

How to change a value of a tuple in Python

Python is the language for newly evolving technologies and has become one of the most popular programming languages in the world. It is used in everything right, from simple algorithm...

3 minutes read.

Convert uppercase to lowercase in Python

Python String lower() By using the built-in string lower() method, all the uppercase characters can be converted into lowercase characters in a string, The lowercased string from the given string is returned...

1 minute read.

Spark and Python for big data with pyspark github

Pyspark: Apache Spark is a computational engine that handles large data sets using parallel and batch processing. Apache Spark is an open-source, distributed computing framework and collection of libraries for real-time,...

3 minutes read.

Python oct() function

Python oct() function The oct() function in Python converts an integer number to an octal string prefixed with “0o”. Syntax oct(x) Parameter x: This parameter represents an Integer Number Return This function returns an octal string. Example 1 #...

1 minute read.

Data Distribution in python

Before discussing data distribution, we will discuss each word that is data and Distribution. What is meant by data? A collection of raw facts and figures is called data. The word "raw"...

18 minutes read.

Sklearn in Python

Scikit-learn or sklearn is a machine learning library used in Python that provides many unsupervised and supervised learning tools and algorithms. David Cournapeau first created it as a 2007 Google...

7 minutes read.

Python Set difference() Method

Python Set difference() Method The set.difference() method in Python returns the set difference of two sets(A-B). Syntax set.difference(set1) Parameter set- This argument represents a set (minuend) set1- This arguments represents a set(subtrahend) Return This method returns the difference of the two specified...

2 minutes read.

Image to Text in python

Processing out information from an image is a very big task in all the fields of work such as in development and business sector. The process of converting an electronical...

3 minutes read.

Python Dictionary values() method

Python Dictionary values() method The dictionary.values() method in Python returns a view object that displays a list of all the values in the specified dictionary. Syntax dictionary.values() Parameter NA Return This method returns a view object. The...

1 minute 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 coding platform

Python is a popular general-purpose programming language with many applications. High-level data structures, datatypes, dynamic binding, and many other features make it useful for both designing complex applications and "glue...

6 minutes read.

Mrcnn Python

Python Programming Language 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...

6 minutes read.

How to Make an App with Python

In this age of mobiles, rapid mobile application development has got a lot of traction. Moreover, app developers are high in demand because of the increase in digitization. Generally, Python is...

4 minutes read.