1. What is HTTP?

HTTP (HyperText Transfer Protocol) is a communication protocol used for data exchange between clients (browsers) and servers.


2. HTTP Request

Structure of an HTTP Request

GET /api/users HTTP/1.1
Host: example.com
User-Agent: Mozilla/5.0
Accept: application/json

Component Description
Method Defines the request type (GET, POST, PUT, DELETE)
URL Target resource (e.g., /api/users)
HTTP Version HTTP/1.1, HTTP/2, HTTP/3
Headers Request metadata (Accept: application/json)
Body Present in POST, PUT requests, contains request data

Common HTTP Methods

Method Purpose
GET Retrieve a resource (safe, no side effects)
POST Create a new resource (non-idempotent)
PUT Update a resource (idempotent)
PATCH Partially update a resource
DELETE Remove a resource

3. HTTP Response

Structure of an HTTP Response

HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 123

{
  "id": 1,
  "name": "John Doe"
}

Component Description
Status Code Indicates request success/failure (200 OK, 404 Not Found)
Headers Response metadata (Content-Type: application/json)
Body Data returned by the server

Common HTTP Status Codes

Category Status Code Meaning
Success (2xx) 200 OK Request successful
201 Created Resource created
Redirection (3xx) 301 Moved Permanently Permanent redirect
302 Found Temporary redirect
Client Errors (4xx) 400 Bad Request Invalid request
401 Unauthorized Authentication required
403 Forbidden Access denied
404 Not Found Resource not found
Server Errors (5xx) 500 Internal Server Error Server-side issue
503 Service Unavailable Server temporarily down

4. HTTP Headers

Header Purpose Example
Content-Type Defines the response format application/json
Authorization Authentication token Bearer token123
Accept Specifies accepted response formats application/json
User-Agent Client information Mozilla/5.0
Cache-Control Controls caching behavior no-cache, max-age=3600