Akshaya Sriram • Nov 12, 2024

Exploring the Components of REST APIs

alt text

In software development, APIs (Application Programming Interface) are essential for enabling applications to communicate and share data with each other.

Among the many types of APIs, REST APIs (Representational State Transfer) have become the backbone of modern web applications. Whether you’re browsing social media, or checking the weather, you’re likely to interact with multiple REST APIs.

In this article, we’ll explore in detail the fundamentals, working and different components of a REST API.

What is a REST API?

Web API acts as an interface between a web server and browser. All web services are APIs but all APIs are not web services. REST API uses a set of functions to access data from the server. The client and server interact through HTTP requests and responses.

Securing a REST API

All API needs to be authenticated and monitored. There are ways to secure a REST API:

  • Authentication Tokens authenticate the user making the API call. It checks the identity and rights to access the API for the particular call.
    Authorization: Bearer eyJhbGciOiJS...
    
  • API Keys verifies the application making the API call. It verifies the application and rights to access the API for the particular call.
    Authorization: Api-Key tqzxsd23145
    
  • OAuth (Open Authorization) is a protocol that offers secure access by using passwords and tokens.

Examples of Rest API

REST APIs are widely used across applications and industries. Here are few real-life examples of REST APIs:

  • Amazon S3 allows developers to incorporate AI functionality into their applications and help secure data exchange between applications by detecting vulnerabilities.
  • Social Media Platforms: APIs provided by Twitter and Instagram allow developers to fetch and post social content. The Twitter API allows developers to integrate Twitter functionality and promote their application through the platform. Similarly, Instagram API provides developers access to user data on the platform.

Components of REST APIs

REST API determines the set of rules for the developers to follow while creating an API. It is possible to fetch data or resources when linked to a URL. URL denotes the request sent and the data sent back is called response.

JSON (JavaScript Object Notation) is a format used to send a request to a server through REST API. It is similar to JavaScript and each object and value must be enclosed in double quotes.

{
  "username": "user_name",
  "password": "password"
}

Client and Request

A client is responsible for sending requests in response to user actions or external events. For example, a user might initiate a request by entering a search term. A request to a REST API is comprised of the following components:

Endpoint

It is the URL of the request to fetch data or resources. The structure of the endpoint has a root-endpoint which is the starting point of API followed by the path which determines the resource requested.

Parameters

Variables passed to an endpoint to provide specific instructions for the API to process. The parameters can be included in the request as part of the URL, in the query string, or in the request body.

Request Method

The function or method to be performed on the resource identified by the URL. It is the type of request sent to the server. It is case-sensitive and should be given in uppercase.

S. NoMethodFunctionDescription
1.GETReadRetrieve information from the server.
2.POSTCreateCreate a new resource or send data to the server.
3.PUT/PATCHUpdate/ModifyUpdate or modify existing resources on the server.
4.DELETEDeleteDelete a resource from the server.
5.HEADReadSimilar to GET, it retrieves the status line and header only.
6.CONNECTSets up a tunnel to the server.
7.OPTIONSLists the communication options for the resource.
8.TRACEPerforms message loopback test.

Request Headers

Used by the client to pass additional information about the request to the server. For example, it can be used for authentication and to provide additional information about the body content.

Request Body

The body or data has the information to be sent to the server. POST or PUT method requires a body and the GET method does not require a body.

Server and Response

The client assembles the request, sends it to the appropriate endpoint on the server for processing. The server is responsible for handling authentication, validating input data, retrieving or manipulating data from a database, and returning the response to the client.

A response comprises of version followed by a status code and associated description. A response from a REST API is comprised of the following components:

Status Code

It is a 3-digit integer where the first digit defines the class of the response and the last two digits do not have any categorization.

CodeMessageDescription
1xxInformational
100ContinuePart of the request received.
101Switching ProtocolServer switching protocol.
2xxSuccessful
200OKRequest successful.
201CreatedNew Resource created.
202AcceptedRequest received but not completed.
3xxRedirection
300Multiple ChoiceMultiple responses for the request.
301Moved PermanentlyRequested page moved to a new URL.
302FoundRequested page moved temporarily to a new URL.
4xxClient Error
400Bad RequestServer could not return a response.
401UnauthorizedUsername and password needed.
404Not FoundServer could not find the requested page.
5xxServer Error
500Internal Server ErrorServer met an unexpected condition.
501Not ImplementedServer did not support the functionality.
504Gateway Timed outGateway has timed out.

Response Headers

Similar to request headers, provide additional information about the server’s response. For example, the set-cookie header instructs to store a cookie in the browser.

Response Body

The body or data has the information returned by the server in response to the client’s request. It typically include structured data objects that represent the requested resources, metadata, and if the request failed - error messages.

Looking Forward

REST APIs continue to be a crucial part of modern web development. Their simplicity, scalability, and flexibility make them a popular choice for developers and organizations.

As technology evolves, staying updated with REST AP and understanding them can help you make better decisions in application development.

References: