HomeBytesheetsRoadmapsUI TemplatesUI ToolsStack Discussion
3 Topics
Chapter•150 Minutes

HTTP Fundamentals

Learn HTTP deeply because REST APIs are built on HTTP semantics.

HTTP Request Structure

An HTTP request consists of a method, target URI, protocol version, headers, and optionally a body.

POST /api/users HTTP/1.1
Host: api.example.com
Content-Type: application/json
Authorization: Bearer eyJ...

{
  "name": "Suriya",
  "email": "suriya@example.com"
}

The method communicates the intended operation. Headers carry metadata. The body carries representation data when appropriate.

The URL itself can contain a path and query string. For example, /users/42?include=orders identifies user 42 while providing an optional query parameter.

Raw HTTP Request
GET /api/users/42?include=orders HTTP/1.1
Host: api.example.com
Accept: application/json
Authorization: Bearer TOKEN
💡 The request asks the server for user 42 and indicates that orders should also be included.
Key Takeaways
  • Methods express intent.
  • Headers carry metadata.
  • The body carries representation data.
  • Query parameters modify retrieval or filtering behavior.

HTTP Response Structure

An HTTP response contains a status code, headers, and optionally a response body.

HTTP/1.1 200 OK
Content-Type: application/json
Cache-Control: max-age=60

{
  "id": 42,
  "name": "Suriya"
}

The status code communicates the broad result of the operation. Headers provide metadata about caching, content type, authentication challenges, rate limits, cookies, and other behavior.

A good API uses status codes consistently rather than returning 200 OK for every situation.

Response Components
Status: 200 OK
Communicates the outcome at the HTTP protocol level.
Headers: Content-Type, Cache-Control
Describe metadata and processing instructions.
Body: JSON representation
Contains the returned resource or result.

HTTP Methods: GET, POST, PUT, PATCH, DELETE and HEAD

GET retrieves a representation and should not intentionally change server state. POST commonly creates a subordinate resource or triggers a server-side operation. PUT replaces the representation of a resource at a known URI and is intended to be idempotent. PATCH applies partial modifications. DELETE removes a resource and is also intended to be idempotent.

HEAD is similar to GET but asks for response headers without the response body. It is useful for checking existence, metadata, content length, or cache information.

Do not select methods simply because they sound convenient. Method semantics affect caching, retries, proxies, client behavior, and API correctness.

HTTP Method Semantics
GET: Retrieve
Safe and idempotent.
POST: Create/process
Usually not idempotent.
PUT: Replace
Idempotent when designed correctly.
PATCH: Partially modify
Used for partial updates.
DELETE: Remove
Idempotent semantics are expected.
HEAD: Retrieve metadata
Returns headers without a response body.
Knowledge Check

Which HTTP method is generally intended to be idempotent and replace a resource representation?