Open In App

REST API Introduction

Representational State Transfer (REST) is an architectural style that defines a set of constraints to be used for creating web services. REST API is a way of accessing web services in a simple and flexible way without having any processing.



REST technology is generally preferred to the more robust Simple Object Access Protocol (SOAP) technology because REST uses less bandwidth, simple and flexible making it more suitable for internet usage. It’s used to fetch or give some information from a web service. All communication done via REST API uses only HTTP request. 

Working: A request is sent from client to server in the form of a web URL as HTTP GET or POST or PUT or DELETE request. After that, a response comes back from the server in the form of a resource which can be anything like HTML, XML, Image, or JSON. But now JSON is the most popular format being used in Web Services. 
 



Build REST API Mastery Learn to integrate popular and practical Python REST APIs in Django web applications with Educative’s interactive skill path Become a Python-based API Integrator. Sign up at Educative.io with the code GEEKS10 to save 10% on your subscription.

In HTTP there are five methods that are commonly used in a REST-based Architecture i.e., POST, GET, PUT, PATCH, and DELETE. These correspond to create, read, update, and delete (or CRUD) operations respectively. There are other methods which are less frequently used like OPTIONS and HEAD.  

NOTE: POST is neither safe nor idempotent. 

Idempotence: An idempotent HTTP method is a HTTP method that can be called many times without different outcomes. It would not matter if the method is called only once, or ten times over. The result should be the same. Again, this only applies to the result, not the resource itself. 

Example:




1. a = 4 // It is Idempotence, as final value(a = 4)
        // would not change after executing it multiple
       // times.
 
2. a++ // It is not Idempotence because the final value
      // will depend upon the number of times the
     // statement is executed.

Request and Response

Now we will see how request and response work for different HTTP methods. Let’s assume we have an API(https://www.geeksforgeeks.org/api/students) for all students data of gfg.

     Request
GET:/api/students

Request

POST:/api/students

{“name”:”Raj”}

Request

PUT or PATCH:/api/students/1

{“name”:”Raj”}

     Request
DELETE:/api/students/1

RESTful web services are very popular because they are light weight, highly scalable and maintainable and are very commonly used to create APIs for web-based applications.


Article Tags :