HTTP Basics and Client-Server Model
- HTTP communication follows a client-server model, where the client (e.g., mobile app) sends requests to the server, and the server responds accordingly.
- URL (Uniform Resource Locator) identifies the location of a resource; related terms include URI and URN, which are technical identifiers, but URL is commonly used for location referencing.
- HTTP is one of many protocols used for communication; protocols like SRV, SRV-W exist too. HTTP mainly transfers data in clear text, while HTTPS encrypts data for security.
- HTTP stands for HyperText Transfer Protocol, primarily designed to transfer text and other data efficiently across the internet, optimizing for lightweight data structures to reduce processing and network load.
HTTP Request and Response Headers
- HTTP headers carry metadata about the request or response, structured as key-value pairs. They provide additional context such as file name, size, creation/modification dates, and more.
- Headers are open and extensible; some are standardized while others can be custom-defined by developers.
- Headers exist in both requests (from client) and responses (from server), indicating details like the source of the request (e.g., browser or Postman), response status, content type, etc.
Common uses of headers include:
- Caching: To check if cached data can be reused.
- Authentication: Using tokens like Bearer tokens, JWT, session cookies, refresh tokens.
- State management: Tracking user status (guest or logged-in), cart contents, etc.
Older headers used an "X-" prefix (e.g., X-Name), but this convention has mostly been deprecated since around 2012-2013.
Headers can be categorized into:
Request headers: Sent by client with data or metadata.
**Response headers: **Sent by server with status and metadata.
**Representation headers: **Indicate encoding or compression of data (e.g., gzip).
Some headers enforce security and access policies (e.g., CORS), but they require explicit implementation in server code and do not act automatically.
Common HTTP Headers Examples
Header Name | Purpose |
---|---|
Accept |
Specifies the media types the client can handle (e.g., application/json ) |
User-Agent |
Identifies the client application or browser making the request |
Authorization |
Contains credentials like Bearer tokens for authentication |
Content-Type |
Indicates the media type of the request or response body (e.g., JSON, PDF) |
Cookie |
Stores key-value pairs for session management |
Cache-Control |
Controls caching policies for requests and responses |
HTTP Methods Overview
HTTP methods specify the type of operation performed on a resource:
GET: Retrieve a resource or list (no message body in request).
**HEAD: **Retrieve only headers of a resource (no body), useful for metadata checks like cache status or user agent info.
**POST: **Send data to create a new resource, commonly used for adding new entries in a database.
**PUT: **Replace an entire resource with the request payload.
*PATCH: * Partially update a resource, modifying only specified fields without replacing the entire object.
*DELETE: * Remove a resource from server.
OPTIONS: Query the server for supported HTTP methods on a specific endpoint, useful for API discovery and CORS preflight requests.
*TRACE: * Used mostly for debugging; echoes back the received request to detect proxies or routing issues.
*GET, POST, PUT, DELETE, and PATCH * are the most commonly used methods; others like OPTIONS and TRACE are less frequent but important for specific use cases.
HTTP status codes are standard response codes given by web servers on the internet. They help indicate whether a request has been successfully completed or if an error occurred. Here's a quick overview of the main categories:
π΅ 1xx β Informational
100 Continue β Request received, continue to send the rest.
101 Switching Protocols β Switching to a different protocol.
π’ 2xx β Success
200 OK β Request succeeded.
201 Created β Resource created.
204 No Content β Successful, but no content to return.
π‘ 3xx β Redirection
301 Moved Permanently β Resource moved to a new URL permanently.
302 Found β Temporary redirect.
304 Not Modified β Resource not changed since last request.
π΄ 4xx β Client Errors
400 Bad Request β Invalid request.
401 Unauthorized β Authentication required.
403 Forbidden β Access denied.
404 Not Found β Resource not found.
429 Too Many Requests β Rate limit exceeded.
π΄ 5xx β Server Errors
500 Internal Server Error β Server encountered an error.
502 Bad Gateway β Invalid response from upstream server.
503 Service Unavailable β Server is down or overloaded.
504 Gateway Timeout β Upstream server didn't respond in time.
HTTP Status Codes and Their Categories
Knowing status codes is essential for backend developers to handle requests and errors properly; exact memorization is unnecessary, but understanding usage and ranges is important.
Practical Insights and Learning Approach
Understanding HTTP deeply involves knowledge of data structures, operating systems, networking, and cryptography, as these affect how data is transferred, processed, and secured.
Developers should gain a broad overview of HTTP before writing backend controllers; this foundational knowledge distinguishes good programmers from average ones s s .
Tools like Postman or Thunder Client are commonly used to test and explore HTTP methods and headers interactively s s .
The learning journey includes exploring network latency, data optimization, and efficient communication strategies ... .
π‘ Key Insight: HTTP is fundamentally about transferring text and data efficiently and reliably between clients and servers using defined methods, headers, and status codes. Mastery of these basics enables building robust web applications and services.
Top comments (0)