Day 4 — HTTP Fundamentals#

Goal: Understand how the web works at the protocol level — HTTP methods, status codes, headers, and request/response structure — so you can debug APIs and build web services.


What Is HTTP?#

HTTP (HyperText Transfer Protocol) is the language that browsers and servers speak. Every time you:

  • Open a web page
  • Submit a form
  • Fetch data from an API
  • Upload a file

…an HTTP request goes out and an HTTP response comes back.

Client (browser/script)              Server
     │                                 │
     │──── HTTP Request ──────────────▶│
     │     GET /api/users              │
     │     Host: example.com           │
     │                                 │
     │◀─── HTTP Response ─────────────│
     │     200 OK                      │
     │     [{"name": "Alice"}, ...]    │

HTTP Methods — What Action Are You Requesting?#

MethodPurposeExample
GETRead dataGet a list of users
POSTCreate something newCreate a new user
PUTReplace an entire resourceReplace user data completely
PATCHUpdate part of a resourceUpdate just the email
DELETERemove a resourceDelete a user

The CRUD mapping#

OperationHTTP MethodSQL equivalent
CreatePOSTINSERT
ReadGETSELECT
UpdatePUT / PATCHUPDATE
DeleteDELETEDELETE

Examples#

GET    /api/users           → Get all users
GET    /api/users/42        → Get user with ID 42
POST   /api/users           → Create a new user
PUT    /api/users/42        → Replace user 42's data
PATCH  /api/users/42        → Update user 42's email only
DELETE /api/users/42        → Delete user 42

GET requests should never change data. GET is for reading only. If an API changes data on GET, it’s poorly designed.

🧠 Knowledge Check#

Q1: Which HTTP method is best suited for updating a specific field (like a user’s email) without replacing their entire profile?

  • A) GET
  • B) POST
  • C) PUT
  • D) PATCH
Answer

DPATCH is used for partial updates, whereas PUT typically replaces the entire resource.


HTTP Status Codes — What Happened?#

Status codes are three-digit numbers in the response that tell you the result.

The Ranges#

RangeCategoryMeaning
1xxInformationalRequest received, continuing…
2xxSuccessRequest succeeded
3xxRedirectionGo somewhere else
4xxClient ErrorYou made a mistake
5xxServer ErrorThe server broke

Codes You Must Know#

CodeNameWhat it means
200OKRequest succeeded (GET returned data)
201CreatedResource was created (POST succeeded)
204No ContentSuccess, but nothing to return (DELETE)
301Moved PermanentlyURL has permanently changed
302FoundTemporary redirect
400Bad RequestYour request is malformed (typo, wrong JSON)
401UnauthorizedYou need to authenticate (login)
403ForbiddenYou’re authenticated but not allowed
404Not FoundThe resource doesn’t exist
405Method Not AllowedWrong HTTP method (POST to a GET-only endpoint)
422Unprocessable EntitySyntax OK but semantically wrong
429Too Many RequestsRate limited — you’re sending too fast
500Internal Server ErrorServer crashed — not your fault
502Bad GatewayServer behind a proxy crashed
503Service UnavailableServer is overloaded or down for maintenance

Memory aids#

2xx = 😊 Everything is fine
4xx = 😬 You messed up (fix your request)
5xx = 💥 Server messed up (try again later or contact admin)

🧠 Knowledge Check#

Q1: You made a request to /api/users and received a 404 status code. What does this mean?

  • A) You are not authorized to view the users
  • B) The server crashed while processing your request
  • C) The resource /api/users does not exist
  • D) You sent the request too fast
Answer

C404 Not Found means the URL you requested doesn’t point to an existing resource.


Anatomy of an HTTP Request#

POST /api/users HTTP/1.1          ← method, path, version
Host: api.example.com             ← headers start here
Content-Type: application/json
Authorization: Bearer eyJhbGci...
Accept: application/json
                                   ← empty line separates headers from body
{                                  ← request body (for POST/PUT/PATCH)
    "name": "Alice",
    "email": "[email protected]"
}

Key parts#

PartWhat it is
MethodGET, POST, PUT, PATCH, DELETE
PathThe URL path (e.g., /api/users)
HeadersMetadata about the request
BodyData you’re sending (not used in GET)

Anatomy of an HTTP Response#

HTTP/1.1 201 Created              ← status line
Content-Type: application/json
Date: Mon, 15 Jan 2026 10:30:00 GMT
X-Request-Id: abc-123
                                   ← empty line
{                                  ← response body
    "id": 42,
    "name": "Alice",
    "email": "[email protected]",
    "created_at": "2026-01-15T10:30:00Z"
}

Common HTTP Headers#

Request Headers (you send)#

HeaderPurposeExample
Content-TypeFormat of the body you’re sendingapplication/json
AcceptFormat you want backapplication/json
AuthorizationAuthentication credentialsBearer <token>
User-AgentIdentifies your clientMozilla/5.0... or python-httpx/0.27
CookieSession cookiessession_id=abc123

Response Headers (server sends)#

HeaderPurposeExample
Content-TypeFormat of the response bodyapplication/json
Content-LengthSize of the response in bytes1234
Set-CookieTells browser to store a cookiesession=xyz; HttpOnly
Cache-ControlCaching rulesmax-age=3600
X-RateLimit-RemainingAPI rate limit info48

JSON — The Language of APIs#

Most APIs speak JSON (JavaScript Object Notation):

{
    "name": "Alice",
    "age": 25,
    "is_student": true,
    "courses": ["TDS", "PDSA"],
    "address": {
        "city": "Chennai",
        "state": "Tamil Nadu"
    }
}

JSON types#

TypeExample
String"hello"
Number42, 3.14
Booleantrue, false
Nullnull
Array[1, 2, 3]
Object{"key": "value"}

JSON in Python#

import json

# Python dict → JSON string
data = {"name": "Alice", "age": 25}
json_str = json.dumps(data)
print(json_str)  # '{"name": "Alice", "age": 25}'

# JSON string → Python dict
parsed = json.loads(json_str)
print(parsed["name"])  # Alice

Authentication — How APIs Know Who You Are#

1. API Keys#

A simple token sent in a header or query parameter:

# In header:
curl -H "X-API-Key: abc123" https://api.example.com/data

# In query parameter:
curl "https://api.example.com/data?api_key=abc123"

2. Bearer Tokens (JWT)#

A signed token sent in the Authorization header:

curl -H "Authorization: Bearer eyJhbGciOiJIUzI1NiJ9..." https://api.example.com/data

3. Basic Auth#

Username and password encoded in Base64:

curl -u "username:password" https://api.example.com/data
# Equivalent to: Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=

Q&A#

Q: What is the difference between 401 and 403?

A:

  • 401 Unauthorized — “I don’t know who you are.” You haven’t provided credentials, or they’re invalid. Fix: add/fix your authentication.
  • 403 Forbidden — “I know who you are, but you’re not allowed.” You’re authenticated but don’t have permission for this resource.
Q: When should I use PUT vs PATCH?

A:

  • PUT — sends the complete resource. Everything you don’t include gets reset/deleted.
  • PATCH — sends only the changes. Fields you don’t include stay as they are.

Example: updating a user’s email:

// PUT /users/42 — must send EVERYTHING:
{"name": "Alice", "email": "[email protected]", "age": 25}

// PATCH /users/42 — send only what changed:
{"email": "[email protected]"}
Q: What does "rate limiting" (429) mean?

A: The API limits how many requests you can make per second/minute/hour. If you exceed the limit, you get a 429 response. Fix: slow down, add delays between requests, or check the Retry-After header.

Q: What is the difference between HTTP and HTTPS?

A: HTTPS = HTTP + TLS encryption. With HTTPS, the data between you and the server is encrypted — no one in the middle can read it. Always use HTTPS for APIs that involve authentication or sensitive data.

Q: Can GET requests have a body?

A: Technically, the HTTP spec doesn’t forbid it, but practically no. GET requests should not have bodies. If you need to send data, use POST. If you need to filter data with GET, use query parameters: GET /users?role=admin&limit=10.


Exercises#

Exercise 1: Status code identification

For each scenario, what status code would you expect?

  1. You request a user profile and get the data back
  2. You try to access an admin endpoint without logging in
  3. You send a POST request to create a new item, and it’s created
  4. You request /api/users/99999 but user 99999 doesn’t exist
  5. The server crashes while processing your request
Answers
  1. 200 OK — successful GET request
  2. 401 Unauthorized — no authentication provided
  3. 201 Created — successful POST that created something
  4. 404 Not Found — resource doesn’t exist
  5. 500 Internal Server Error — server-side crash

Exercise 2: Method identification

What HTTP method would you use for each action?

  1. Viewing a list of all blog posts
  2. Publishing a new blog post
  3. Changing the title of an existing blog post
  4. Deleting a comment
  5. Replacing an entire user profile with new data
Answers
  1. GET /api/posts — reading data
  2. POST /api/posts — creating new data
  3. PATCH /api/posts/42 — updating part of a resource
  4. DELETE /api/comments/17 — removing a resource
  5. PUT /api/users/5 — completely replacing a resource

Exercise 3: Read the response

Given this HTTP response, answer the questions:

HTTP/1.1 403 Forbidden
Content-Type: application/json
X-RateLimit-Remaining: 0

{"error": "Access denied", "message": "Upgrade to premium for this endpoint"}
  1. Was the request successful?
  2. What does the status code mean?
  3. What format is the response body in?
  4. Why might you be getting this error?
Answers
  1. No — 403 is a client error (4xx range)
  2. Forbidden — the server knows who you are but won’t let you access this resource
  3. JSON (Content-Type: application/json)
  4. The message says “Upgrade to premium” — this endpoint requires a paid subscription. Also, X-RateLimit-Remaining: 0 suggests you may have hit your rate limit.

Exercise 4: MCQ

Q1: A 201 Created response is returned after which type of request?

  • A) GET
  • B) DELETE
  • C) POST
  • D) OPTIONS
Answer

C201 Created is typically returned after a successful POST request that creates a new resource.


Q2: You get a 500 Internal Server Error. What should you do?

  • A) Check your request headers for typos
  • B) Wait and try again — it’s a server-side issue
  • C) Change the HTTP method
  • D) Delete your API key and create a new one
Answer

B500 means the server had a problem, not your request. Try again later, or contact the API provider if it persists.


Q3: Which header tells the server what format you’re sending data in?

  • A) Accept
  • B) Content-Type
  • C) Authorization
  • D) User-Agent
Answer

BContent-Type describes the format of the request body (e.g., application/json). Accept tells the server what format you want in the response.