×

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

Enumerate() Function in Python

The enumerate() function in Python is used to convert a data collection object into an enumerate object. It returns an object that contains a counter as a key for each...

3 minutes read.

Python Tutorial

Python tutorial is a widely used programming language which helps beginners and professionals to understand the basics of Python programming easily. Python is a high-level, easy, interpreted, general-purpose, and dynamic programming...

19 minutes read.

Python dir() function

Python dir() function The dir() function in Python returns all properties and methods of the specified object, without the values. Syntax dir([object]) Parameter object: This parameter represents the object one wants to see the valid attributes. Return This function...

2 minutes read.

Python Strong Number

Strong Number- Logic Divide each of the number's digits into separate units to determine if it is a strong number or not.The factorial of each digit must then be determined. The...

5 minutes read.

Python Syntax Error Invalid Syntax

Python is renowned for its simple and direct syntax. However, we might come across some things that Python doesn't allow if we are learning Python for the very first time or if...

14 minutes read.

Returning Multiple Values in Python

Python is considered a general-purpose programming language; it is a high-level programming language that is not much difficult and easier to learn. It is rich in libraries that can be...

3 minutes read.

How to Call a Function in Python

How To Call a Function in Python Functions are the well-defined and structured piece of code that is used to implement specific functionality. Calling a function in python is the best...

4 minutes read.

GUI Calendar using Tkinter in Python

GUI: A graphical interface (GUI) is a user interface that lets users interact with electronic devices like computers and smartphones by using menus, icons, and other visual cues (graphics). In contrast...

3 minutes read.

Python set()

Python set() Class The set() class in Python returns a new set object, optionally with elements taken from iterable.  Syntax class set([iterable]) Parameter iterable : This parameter represents a sequence, collection or an iterator object Return This function returns a...

1 minute read.

wxPython Panel class

wxPython Panel class The Widgets which is shown in the frame of GUI window such as text box, buttons, static text etc. are put inside the panel class of the wxpython...

2 minutes read.

Python Tkinter Tutorial

What is Tkinter? Tkinter is GUI library of Python. When we integrated Tinkter with Python, it provides an easy and quick way to develop GUI applications. It provides the Tk GUI...

14 minutes 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.

Read numpy array in Python

Numpy is a numerical python that deals with multidimensional arrays mostly used in storing multiple values. Python's core scientific computing package is called NumPy. This Python library provides multidimensional array...

9 minutes read.

Sort Dictionary in Python

Dictionaries In Python, a dictionary is an unordered collection or set of data types that enable of store data in an unordered key-value/pair. The key is stored alongside with value. Dictionary contains key:...

4 minutes read.

Python List

The list is one of the most versatile, mutable data-structures in Python. It can store heterogeneous data or different types of data. The list contains comma-separated values (item) within the square brackets. Creating...

4 minutes read.

Python Filter List

To filter a list, we use filter () method function. The filter () will test every element true or not in the sequence. Syntax: Filter(function, sequence) Parameters: Function: Function that check and verifies if...

2 minutes read.

Python int()

Python int() class The int() class in Python returns an integer object constructed from a number or string x. Syntax class int(x, base=10) Parameter x: This parameter represents a number or a string that can be converted into...

1 minute read.

Syntax of Map function in Python

In this tutorial, we will understand what the map function in Python is meant. Further, we will see the syntax to be used for the same in python language. We...

3 minutes read.

Python ord() Function

Python ord() Function The ord() function in Python returns the number representing the Unicode code of a specified character. Syntax ord(c) Parameter c: This parameter represents a string or any character. Return This function returns the number...

1 minute read.

Python Deep Copy and Shallow Copy

Assignment statements in Python create bindings between a target and an object, not copies of them. The = operator only makes a new variable that shares the reference to the...

3 minutes read.