What is REST API?

  • REST stands for Representational State Transfer
  • Uses HTTP and HTTP Standard request types to perform operations  on resource exposed by server
    • GET
    • PUT
    • POST
    • DELETE

What is a Resource?

Resource is a Domain Object around which API are exposed.

Resource Examples Consider you are creating School Management Application, here resources will be:

  • Student
  • Teacher
  • ReportCard
  • School
  • Subject

Which HTTP request type is used for what kind of operation on resource in REST API?

  • GET
    • Query specific resource
    • Query collection of resources
  • PUT
    • Updating a resource
  • POST
    • Creating a resource
  • DELETE
    • Deleting a resource

REST stands for: Representational State Transfer, what do you mean by Representational State Transfer?

As per REST standards, Client and Server exchange Representation of Resource

  • When creating a new resource, client sends representation of new resource and server saves it.
  • When client wants its existing resource, it asks server for it and server sends representation of existing state of resource.
  • When client wants to update state of resource on server side, it sends updated representation of resource.

Representation types

  • XML
  • JSON
  • ProtoBuf

Representation being transferred contains:

  • Data
  • Metadata
  • hypermedia links to transition to more information

Example

Create: Client sends data to Server

{
    name: Rakesh Kalra,
    age: 14
    course: science,
}

Get

{
    id: 1211
    name: Rakesh Kalra,
    age: 14
    course: science,
    createTime: 12-05-2020
}

Get All

[
    {
        id: 1211
        name: Rakesh Kalra,
        age: 14
        course: science,
        createTime: 12-05-2020
    },
    {
        id: 1222
        name: Harish Singh,
        age: 17
        course: commerce,
        createTime: 19-05-2020
    }
    ....
]

How updation of information works?

  • Client ask server for current state of resource
  • Client sends server updated state of resource

Can you give example of REST API with say example of Student URLs?

OperationRequest TypeURIRequest Body
Get all StudentsGET/api/students
Get specific StudentGET/api/students/123
Create new StudentPOST/api/studentsRequest body will contain representation of new student
Update specific StudentPUT/api/students/123Request body will contain updated representation of specific student
Delete a StudentDELETE/api/students/123

Rakesh Kalra

Hello, I am Rakesh Kalra. I have more than 15 years of experience working on IT projects, where I have worked on varied complexity of projects and at different levels of roles. I have tried starting my own startups, 3 of those though none of it were successful but gained so much knowledge about business, customers, and the digital world. I love to travel, spend time with my family, and read self-development books.