Understanding REST APIs ๐Ÿš€

Understanding REST APIs ๐Ÿš€


3 min read

Let's get started with the Full Forms first

REST stands for Representational State Transfer

API stands for Application Programming Interface

What are REST APIs?

REST APIs are an interface between 2 systems that want to share data.

REST is a standard and can be implemented over different protocols, In most cases, it is implemented via the HTTP protocol, it provides different methods like GET, and POST which makes it easier.

Typically, we want to transfer data from 1 layer of the system to another layer of the system.

Like client -> server or server -> server

Letโ€™s take the example of Restaurants data.

All the restaurant data sits inside a database, so this data is a resource. We want to share this data with the client. The representation of this data which is on the server will be different from the client representation. Meaning there can be multiple representations of the same data.

The most common way to transfer data in REST is via JSON and any REST API implemented over HTTP protocol will be composed of URI

What is URI?

URI stands for Uniform Resource Identifier , It is a technical term used for the names of all resources Connected to the World Wide Web. URIs established protocols over the internet to conduct the connection between resources.

URI looks something like this: https://akshayraichur.com/about/

2 Important Terms:

  • PATH parameter: Variable in URI path that helps in pointing towards specific resource

    Example: https://food[.]in/restaurant/1/total-orders

    Here, 1 is a variable here which points to restaurant 1s total orders

  • QUERY parameter: Variable in URI path that helps in querying/ filtering through a list of resources

    Example: https://food[.]in/restaurant?search=sub

    Here, search is the query param key, and a sub is a value.

HTTP Request methods

HTTP request methods are a set of methods to indicate the desired action to be performed for a given resource.

Few of the HTTP Request methods:

  • GET: read information about resource

  • POST: create a new resource

  • PUT: update the resource (if exists, else creates new)

  • PATCH : update the resource

  • DELETE: delete the resources.

These are the main HTTP methods used. (There are other methods as well for different use cases)

While the GET the method only fetches the resources, methods like Put, Patch & Post can modify a particular resource or create a new resource.

So, with these methods, we send data back with the request in the form of request payloads and we get an acknowledgment from the server which we call the response body.

HTTP Status Codes

With every REST API implemented over HTTP, we get something called Response codes/ HTTP status codes, these are important because they inform the client about the type of response received.

So there are response codes like:

  • 2xx: Success

  • 3xx: Redirection

  • 4xx: Problem with the client

  • 5xx: Problem with the server

Another important part of REST over HTTP is HTTP headers.

They are the set of attributes that stores the metadata associated with the API.

For example, if we want to send and accept responses in English, then we set Accept-language: โ€˜en-USโ€™

There are headers related to cache and authorization. Remember that headers are not related to resource data.

So, these are a few concepts that we have to keep in mind w.r.t REST APIs.

Hope you learned something new today.

If you like my content, please consider following me on Linkedin, GitHub