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.
GET /api/users/42?include=orders HTTP/1.1
Host: api.example.com
Accept: application/json
Authorization: Bearer TOKEN- 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.
200 OKContent-Type, Cache-ControlJSON representationHTTP 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.
RetrieveCreate/processReplacePartially modifyRemoveRetrieve metadataWhich HTTP method is generally intended to be idempotent and replace a resource representation?