HTTP (Hypertext Transfer Protocol) Basics
HTTP is the communication protocol used to transfer data between web servers and web clients (primarily web browsers). It forms the foundation for all data transmission on the World Wide Web, including HTML documents, images, videos, and JSON data.
1. Core Principle: Request and Response
HTTP communication adheres to the Client-Server model, fundamentally consisting of a Request and a Response pair.
- Client Request: The browser (client) asks the server for a specific resource (web page, file, etc.). The request uses a Method (GET, POST, etc.) and a URL.
- Server Response: The server processes the request and sends the requested resource, along with a Status Code indicating the outcome of the request, back to the client.
2. Key Characteristics of HTTP
- Stateless: HTTP is inherently stateless. Each request is treated as independent; the protocol does not remember any previous requests or states. (Technologies like Cookies and Sessions are used to manage state).
- Connectionless: After the client sends a request and the server sends a response, the connection is immediately terminated. (A design choice to conserve resources).
3. Key HTTP Status Codes
These are 3-digit numbers sent by the server to the client, indicating the result of the request.
| Class | Code Range | Meaning | Example Code | Description |
|---|---|---|---|---|
| 1xx | Informational | Request received, continuing process | 100 |
Continue |
| 2xx | Success | The request was successfully received, understood, and accepted | 200 |
OK (Success) |
| 3xx | Redirection | Further action needs to be taken to complete the request | 302 |
Found |
| 4xx | Client Error | The request contains bad syntax or cannot be fulfilled | 404 |
Not Found |
| 5xx | Server Error | The server failed to fulfill an apparently valid request | 500 |
Internal Server Error |
Commonly Used Codes
| Code | Description | Meaning |
|---|---|---|
| 200 OK | The request has succeeded and the response body is transferred. | Most common success response |
| 201 Created | The request has succeeded and a new resource has been created. (Typically the result of a POST request) |
API creation successful |
| 400 Bad Request | The server cannot process the request due to a client error (e.g., malformed syntax). | Client data error |
| 401 Unauthorized | The client lacks valid authentication credentials. | Authentication required |
| 403 Forbidden | The client is authenticated but does not have permission to access the resource. | Insufficient permission |
| 404 Not Found | The requested resource (URL) could not be found on the server. | Most common error |
| 503 Service Unavailable | The server is not ready to handle the request, often due to maintenance or overload. | Server temporarily down |