Take some REST


REpresentational State Transfer!

What is REST? (REST API)

  • Let’s say you’re trying to find videos about Avengers on Youtube. You open up Youtube, type “Avengers ” into a search field, hit enter, and you see a list of videos about Avengers. A REST API works in a similar way. You search for something, and you get a list of results back from the service you’re requesting from.
Wait...

What is an API?
  • API stands for Application Programming Interface which will have a set of rules that allows the programs to talk to each other over a protocol. The developer creates the API on the server (host) 
API

One of the rules in REST states that the user should be able to get a piece of data, which is called resource, when linked to a specific URL. When doing this, certain security filtering may done when user tries to get to the resource through that URL. For example, application might filter only logged in users to view certain resources.

Each linking to the URL made is called a request to the resource and a response is sent. The response can either be any of the below stuff
  1. The requested data OR
  2. Any Error occurred while fetching the requested data
  3. Status code of the response payload.
Let's look at the blueprint of a typical Request

A request contains the following
  • The Route
  • Request Method
  • Headers
  • The Body (also called data)

The Route

A typical route would look like this. A unique root will display/ do an action on the requested data ( more on actions/ methods below)

localhost:PORT/path/:params
The path determines the resource you’re requesting for.

The Request Method

The methods are used for performing actions on the resource/s found on the specific URL.Which also means performing CRUD (Create, Read, Update, Delete) actions on the resource. Below are the commonly used methods used in RESTful web services
  • GET (Read)
    • This stands for reading the data/resource. This can be used in two main ways.
      1. Get details of all the resources
        • The below is an example of a URL used in get method. Here we are not fetching the details of a single resource but fetching details of all resources

        • localhost:PORT/resource
          OR 
          localhost:PORT/resource/all
          
          

      2. Get details of a single resource
        • The below is also an example of a URL used in get method. Here we are not fetching all the details of a resource/s but fetching details of a single resource.

          localhost:PORT/resource/:param
        • In the URL, 'params' stands for any ID or unique property of a resource which could uniquely  identify a single record/data 
  • POST (Add)
    • The URL for POST requests would look something like the below code and also the request body (more on that below) contains the data to be added.

      localhost:PORT/resource/add

  • PUT/PATCH (Update)
    • In PUT method, we update the details of a resource, given the ID of the resource is provided in the URL as 'params'

      localhost:PORT/resource/edit/:params
  • DELETE
    • Delete method could be executed as below
      • Either to delete a resource

        localhost:PORT/resource/delete/:params
      • Or to delete entire collection of resources

        localhost:PORT/resource/delete

The Header

  • Headers are used to provide information to both the client and server. It can be used for many purposes, such as authentication and providing information about the body content.

Header of a request

The Body(or Data)

  • The data (sometimes called “body” or “message”) contains information you want to be sent to the server. This option is only used with POSTPUTPATCHor DELETE requests.
Illustration of  request body (in JSON)

That's All for this post!
Your comments and feedback are welcome :)
Manoj Kumar D





Comments

Popular posts from this blog

JavaScript!

HTTP Status codes

Node.js