×

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

Python MySQL Update Operation

Python MySQL Update Operation: In this part of tutorial, we will learn that how can we update a table present in SQL database through our Python program. As like SQL,...

4 minutes read.

Parameter Passing in Python

In Python, when we characterize capabilities with default values for specific boundaries, it is said to have its contentions set as a possibility for the client. Clients can either pass their...

3 minutes read.

Fsolve in 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...

3 minutes read.

Reverse a String in Python

Python is an object-oriented high-level programming language. Python has dynamic semantics and has high-level built-in data structures which support dynamic typing and dynamic binding. Python provides rapid development. It has...

4 minutes read.

Python range() function

Python range() function The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and ends at a specified number. Syntax range(stop)        or range(start, stop[, step]) Parameter start: It is...

1 minute read.

Python NewLine

In python, a new line character denoted by "\n" is known as an escape character or escape sequence, and it will force the cursor to change its position to the next...

6 minutes read.

Python program to find the greatest among two numbers

Python program to find the greatest among two numbers We have many approaches to get the largest number among the two numbers, and here we will discuss a few of them....

2 minutes read.

How to create a file in Python?

How to create a file in python? Files provide us a convenient way of storing our information. Once we have our data, the next task is to store it so that...

3 minutes read.

Sentence to python vector

Conversion of a Sentence to Vector in Python Before starting the tutorial, let’s just recap about the vector and the respective package that has to be imported in Python. Python Vector: Putting simply,...

3 minutes read.

Python Set intersection() method

Python Set intersection() method The set.intersection() method in Python returns a new set with elements that are similar between two or more sets. Syntax set.intersection(set1, set2 ... etc) Parameter set1- This parameter represents the set to search for...

2 minutes read.

NOT IN operator in Python

This page contains all the information about “not in” operator in python. You can also know everything about what type of operator is “not in” in python language and where...

7 minutes read.

Classes & Objects in Python

Classes and Objects are used in most modern programming languages, mainly in Object-oriented Programming languages. What is meant by Object Oriented Programming language? Usage of objects and their components in order to...

7 minutes read.

Python email utils

Python is a popular programming language in this growing world. There are many resources to learn python online without spending a single penny. In this article, we will talk about a...

4 minutes read.

Python | a += b is not always a = a + b

a += b in Python doesn't always behave the same way as a = a + b; the same operands can produce different outcomes depending on the circumstances. But we...

3 minutes read.

Python String Negative Indexing

This page contains all the information how to use “Negative indexing“ in Python with the help of using slicing. What is an Index? An index is a numeric value, which is assigned...

3 minutes read.

Read Text files in Python

In Python, there are many ways to read text files. Before going into the detailed structure of reading a text file, let us understand how reading text files takes place...

4 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.

Looping through Data Frame in Python

Iterate over Rows and Columns in Pandas Dataframe This tutorial aims to make us understand what Pandas in Python are, what Data Frame in Python is and its significance, what are...

4 minutes read.

Python SymPy

A Python package for symbolic mathematics is called SymPy. Its goal is to develop into a fully-fledged computer algebra system (CAS) while maintaining the code as straightforward as possible to...

3 minutes read.

How to check the version of the Python Interpreter?

As we all know what an interpreter is, and how important it is. We should also be aware of the fact that it is important to have knowledge of the...

2 minutes read.