Spots

HTTP Fundamentals & REST Conventions

This week, I learned the fundamentals of HTTP (HyperText Transfer Protocol) and REST (Representational State Transfer) API conventions.

The main focus was understanding how a client

The main focus was understanding how a client communicates with a server through HTTP requests and responses, how different HTTP methods are used, how status codes communicate the result of a request, and how REST APIs should be structured using predictable resource-based URLs.

As part of the practical task, I extended

As part of the practical task, I extended a raw Node.js HTTP server to follow REST conventions by implementing GET, POST, PUT, and DELETE routes with appropriate status codes and HTTP headers. 1. HTTP Request and Response Cycle HTTP is a protocol used for communication between a client and a server. A typical communication follows this process: For example, when a client requests a list of users: The server processes the request and sends a response: An HTTP request can contain: Method – GET, POST, PUT, PATCH, DELETE URL/Path – identifies the requested resource Headers – provide additional information Body – contains data sent to the server when required The response generally contains: HTTP methods define what operation the client wants to perform. GET is used to retrieve data. It can return a list of users. It can return a specific user.

POST is generally used to create a new

POST is generally used to create a new resource. If the user is successfully created, the server can return: PUT is used to replace or completely update a resource. PATCH is used when only part of a resource needs to be updated. For example, if only the name needs to change: DELETE is used to remove a resource. If the deletion is successful and there is no response body, the server can return: One important concept I learned was idempotency. Idempotency means that making the same request multiple times has the same intended final effect as making it once. If this same request is sent multiple times, the final state of user 10 remains the same. This property is useful because HTTP requests can sometimes be retried due to network problems. Idempotency helps make API behavior more predictable when requests are repeated.

HTTP status codes tell the client what happened

HTTP status codes tell the client what happened when the server processed a request. The major categories are: For this week's practical work, I used the following status codes. Used when a request is successfully processed. Used when a new resource has been successfully created. Used when the request succeeds but the server does not need to return a response body. Used when the client sends invalid or malformed data. The server can respond with: Used when the requested resource does not exist. If user 999 does not exist: HTTP headers provide additional information about a request or response. Content-Type tells the client what type of data is being sent. This tells the client that the response body contains JSON data. Cache-Control provides instructions related to caching. Caching can reduce unnecessary requests and improve performance when used appropriately.

The Authorization header is commonly used to send

The Authorization header is commonly used to send authentication credentials or tokens. This allows the server to identify and authorize the client when authentication is implemented. Another important REST concept I learned is statelessness.

A stateless API treats each request independently. The

A stateless API treats each request independently. The server should not need to depend on information stored from a previous request in order to understand the current request. The request contains the information required by the server to process it.

Statelessness makes APIs easier to scale and reason

Statelessness makes APIs easier to scale and reason about because requests can be handled independently by different server instances. 7. REST Resource Naming Conventions REST APIs generally represent resources using nouns, rather than putting actions in the URL. These URLs contain actions such as get, create, and delete. Here, the URL identifies the resource: and the HTTP method describes the operation. This makes APIs easier to understand and maintain. 8. REST API Route Structure A simple user API can follow this structure: The important idea is that the resource name remains /users, while the HTTP method determines the operation. 9. Practical Implementation For the JavaScript cohort, I extended the raw Node.js HTTP server using the built-in http module.

The server was structured around REST-style routes: I

The server was structured around REST-style routes: I also implemented appropriate response status codes: Basic headers were also included: The goal was not just to make the routes work, but to make them follow common REST conventions. After implementing the routes, I reviewed the API URLs to check whether they followed REST naming conventions. The HTTP method communicates the operation: This separates the resource from the operation and makes the API more predictable.

Through this week's learning and practical implementation, I

Through this week's learning and practical implementation, I understood: How the HTTP request/response cycle works The purpose of HTTP methods The difference between GET, POST, PUT, PATCH, and DELETE The concept of idempotency HTTP status code categories When to use 200, 201, 204, 400, and 404 The purpose of HTTP headers Content-Type, Cache-Control, and Authorization The concept of stateless APIs REST resource naming conventions How to design resource-based API endpoints How to extend a raw Node.js HTTP server with REST-style routes How to review existing API routes against REST conventions HTTP and REST conventions provide a standard way for clients and servers to communicate.

News

HTTP Fundamentals & REST Conventions

This week, I learned the fundamentals of HTTP (HyperText Transfer Protocol) and REST (Representational State Transfer) API conventions.

@spots #dev
Source: Dev.to
See more like this