Responses

← Back to Reference

Crafting Effective HTTP Responses

Learn to create structured, informative responses that improve client-server communication reliability.

1. Response Structure

Status Line


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

                        

Body Examples


{
  "users": [
    {"id": 1, "name": "Alice"},
    {"id": 2, "name": "Bob"}
  ],
  "meta": {"total": 2}
}

                        

2. Response Types

200 OK

Successful request with standard response body

304 Not Modified

ETag match, response body ommitted

400 Bad Request

Client sent invalid request parameters

503 Service Unavailable

Server temporarily overload

Successful Flow

Request → Server → Response
(Status + Headers + Body)

Error Handling

Bad Request → Server → 400
{"error": "Invalid JSON"}

3. Practical Examples

201 Created


HTTP/1.1 201 Created
Location: /users/123
Content-Type: application/json
Content-Length: 46

{"id": "123", "name": "New User"}

                        

401 Unauthorized


HTTP/1.1 401 Unauthenticated
WWW-Authenticate: Bearer realm="api", error="invalid_token", error_description="Token signature invalid"

                        

4. Response Lifecycle

Response Flow

Header
Body
Trailers

Compression Diagram

GZIP: Content-Encoding: gzip
Brotli: Content-Encoding: br
Uncompressed: Content-Length: 1234