Skip to content

Intro to REST API

Posted on:August 14, 2023 at 05:15 AM

API (Application Programming Interface) is a set of rules that allows the developer’s code to communicate with external applications or service by sending requests or receiving responses without needing to know the internal working of the external system.

It acts as an interface between different applications. They are commonly used to access data, services or features provided by other applications, libraries or platform.

API integration with client, server, database and cloud

A general API is a broader term that can encompass various architectural styles including REST.


Table of contents

Open Table of contents

REST API

A REST API is a specific type of API that follows the principles of REST architectural style.

REST stands for Representational State Transfer. In simple words it means that each request from a client to a server must contain all the information needed for the server to fulfill the request. The server doesn’t retain any client-specific state between requests.

It is commonly used for building webservices that allow different software applications to communicate with each other over internet. It uses HTTP protocol as a communication interface and transfers data through HTTP methods.

REST API in action


Anatomy of REST API

The anatomy of a request in a REST API involves several components that provide information to the server about the action the client wants to perform. A typical REST API request includes:

REST API Methods

In HTTP there are 5 request methods that are commonly used while implementing a REST API.

URI (Uniform Resource Identifier)

In a REST API, URIs are used to represent resources that the client can interact with but it doesn’t necessarily provide the details of how to access it. Following are the two examples of URI:

URL (Uniform Resource Locator)

A URL is a specific type of URI that not only identifies a resource but also provides the means to locate it. URLs include the protocol, domain name, path, and sometimes query parameters, allowing the client to understand how to access the resource over the internet.

Let’s look at the following examples:

Headers

Headers are additional pieces of information included in the HTTP request and response messages exchanged between the client (usually an application or browser) and the server.

There are several types of headers, each serving a specific purpose. Here are some common headers used in a REST API.

  POST /api/users
  Content-Type: application/json
  { "name": "Alice", "email": "alice@example.com" }
GET / api / posts / 123;
Accept: application / json;

Query Parameters

Query parameters are a way to pass additional information to a server when making a request in a REST API. They are included in the URL after a question mark (?) and are used to modify the behavior of the request or filter the results. Some examples are:

  GET /api/products?category=electronics
  GET /api/products?search=phone

Request Body

For POST and PUT requests, the request body contains the data that the client wants to send to the server. The format of the data (e.g., JSON, XML) is specified in the Content-Type header.

Here’s a simple example of a REST API request to create a new user:

  POST /api/users
  Host: example.com
  Content-Type: application/json
  {
    "name": "John Doe",
    "email": "john@example.com"
  }

Let’s break the above examples in bullet points to understand this better:


Summary

In this article, we looked into the world of APIs, showing how they’re like bridges connecting developers’ code and external apps. We then zoom in on REST APIs, breaking down how they follow REST principles, letting different software apps communicate smoothly using HTTP methods. By unpacking REST API parts, from methods and URIs to headers and query parameters, the idea was to give you a starting intro on the architecture of REST APIs.

Resources