Akshaya Sriram • Nov 12, 2024
Exploring the Components of REST APIs
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. No | Method | Function | Description |
---|---|---|---|
1. | GET | Read | Retrieve information from the server. |
2. | POST | Create | Create a new resource or send data to the server. |
3. | PUT/PATCH | Update/Modify | Update or modify existing resources on the server. |
4. | DELETE | Delete | Delete a resource from the server. |
5. | HEAD | Read | Similar to GET, it retrieves the status line and header only. |
6. | CONNECT | Sets up a tunnel to the server. | |
7. | OPTIONS | Lists the communication options for the resource. | |
8. | TRACE | Performs 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.
Code | Message | Description |
---|---|---|
1xx | Informational | |
100 | Continue | Part of the request received. |
101 | Switching Protocol | Server switching protocol. |
2xx | Successful | |
200 | OK | Request successful. |
201 | Created | New Resource created. |
202 | Accepted | Request received but not completed. |
3xx | Redirection | |
300 | Multiple Choice | Multiple responses for the request. |
301 | Moved Permanently | Requested page moved to a new URL. |
302 | Found | Requested page moved temporarily to a new URL. |
4xx | Client Error | |
400 | Bad Request | Server could not return a response. |
401 | Unauthorized | Username and password needed. |
404 | Not Found | Server could not find the requested page. |
5xx | Server Error | |
500 | Internal Server Error | Server met an unexpected condition. |
501 | Not Implemented | Server did not support the functionality. |
504 | Gateway Timed out | Gateway 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:
More blogs for you
Rappo • Akshaya Sriram • Dec 17, 2024
Introducing Startups Catalog: Simplify Startups Discovery
We're excited to unveil the Startups Catalog — a powerful new feature designed to help you discover and connect with cutting-edge startups that are shaping the future of their industries.
Rappo • Akshaya Sriram • Dec 10, 2024
Boost Your Workflow: List of Internal Developer Platform Tools Every Developer Needs
Internal Developer Platforms (IDPs) simplify and unify infrastructure management, boosting productivity and streamlining the software development lifecycle.
Rappo • Akshaya Sriram • Dec 03, 2024
Rappo Explained: Key to Exploring Tools, Products, and Partnerships
Connecting founders with enterprise champions to foster insights, guidance, and lasting business relationships.