Batch 5: Protocols & Security Networking Apache & NGINX Docker ← Full TOC

← Protocols Index

🌐 HTTP & HTTPS — HTTP methods, status codes, headers, HTTPS, TLS handshake

HTTP (HyperText Transfer Protocol) is the foundation of data communication on the web. HTTP/1.1 is text-based, HTTP/2 adds multiplexing, HTTP/3 uses QUIC. HTTPS is HTTP over TLS — always use HTTPS in production.

📋 Reference

ItemSyntax / ValueDescription
GETGET /path HTTP/1.1Retrieve resource — safe, idempotent, cacheable
POSTPOST /path HTTP/1.1Submit data — creates resource, not idempotent
PUTPUT /path HTTP/1.1Replace entire resource — idempotent
PATCHPATCH /path HTTP/1.1Partial update — idempotent
DELETEDELETE /path HTTP/1.1Remove resource — idempotent
HEADHEAD /path HTTP/1.1Like GET but no body — check existence/headers
OPTIONSOPTIONS /path HTTP/1.1List supported methods — used in CORS preflight
200 OK200 OKRequest succeeded
201 Created201 CreatedResource created (POST/PUT)
204 No Content204 No ContentSuccess, no body (DELETE)
301 Moved Permanently301 Moved PermanentlyPermanent redirect — update bookmarks
302 Found302 FoundTemporary redirect
304 Not Modified304 Not ModifiedCached version is current — use it
400 Bad Request400 Bad RequestMalformed request syntax
401 Unauthorized401 UnauthorizedAuthentication required
403 Forbidden403 ForbiddenAuthenticated but not authorized
404 Not Found404 Not FoundResource does not exist
405 Method Not Allowed405 Method Not AllowedHTTP method not supported
409 Conflict409 ConflictState conflict — duplicate, version mismatch
422 Unprocessable422 Unprocessable EntityValidation error on submitted data
429 Too Many Requests429 Too Many RequestsRate limit exceeded
500 Internal Server Error500 Internal Server ErrorServer-side error
502 Bad Gateway502 Bad GatewayUpstream server returned invalid response
503 Service Unavailable503 Service UnavailableServer overloaded or down for maintenance
Content-TypeContent-Type: application/jsonMIME type of request/response body
AuthorizationAuthorization: Bearer token123Auth token in request header
Cache-ControlCache-Control: max-age=3600, publicCaching directive
CORSAccess-Control-Allow-Origin: *Cross-Origin Resource Sharing header
HTTP/2Multiplexed streams, header compression, server pushMultiple requests over one TCP connection
HTTP/3QUIC protocol — UDP-based, 0-RTT connectionFaster than HTTP/2, especially on lossy networks

💡 Example

# HTTP with curl — the universal HTTP client

# ── Basic requests ────────────────────────────────────────────
curl https://jsonplaceholder.typicode.com/posts/1
curl -s https://api.example.com/users | python3 -m json.tool

# Verbose — show full request and response headers
curl -v https://www.mywebuniversity.com/

# HEAD request — check headers without body
curl -I https://www.mywebuniversity.com/

# ── Methods ───────────────────────────────────────────────────
# GET with query params
curl "https://api.example.com/courses?subject=python&limit=10"

# POST JSON
curl -X POST https://api.example.com/courses \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer mytoken123" \
  -d '{"title":"Python 3 Advanced","credits":3,"level":"advanced"}'

# PUT (full replace)
curl -X PUT https://api.example.com/courses/42 \
  -H "Content-Type: application/json" \
  -d '{"title":"Updated Title","credits":4}'

# PATCH (partial update)
curl -X PATCH https://api.example.com/courses/42 \
  -H "Content-Type: application/json" \
  -d '{"credits":4}'

# DELETE
curl -X DELETE https://api.example.com/courses/42 \
  -H "Authorization: Bearer mytoken123"

# ── Python HTTP client ────────────────────────────────────────
import urllib.request, json

# GET
with urllib.request.urlopen('https://jsonplaceholder.typicode.com/posts/1') as r:
    data = json.loads(r.read())
    print(f"Title: {data['title']}")

# POST with requests library (pip install requests)
import requests

resp = requests.post('https://jsonplaceholder.typicode.com/posts',
    headers={'Content-Type': 'application/json'},
    json={'title': 'MyWebUniversity', 'body': 'Free education', 'userId': 1})
print(f"Status: {resp.status_code}")
print(f"Created: {resp.json()}")

# Session with auth (reuse connection + headers)
session = requests.Session()
session.headers.update({'Authorization': 'Bearer mytoken', 'Accept': 'application/json'})

r = session.get('https://api.example.com/courses')
print(r.json())

# ── HTTP status codes in Python ───────────────────────────────
def check_response(resp):
    if resp.status_code == 200: return resp.json()
    elif resp.status_code == 401: raise PermissionError("Not authenticated")
    elif resp.status_code == 403: raise PermissionError("Not authorized")
    elif resp.status_code == 404: raise FileNotFoundError("Resource not found")
    elif resp.status_code == 429: raise RuntimeError("Rate limited — slow down")
    elif resp.status_code >= 500: raise RuntimeError(f"Server error: {resp.status_code}")
    else: resp.raise_for_status()

🏠 Index  |  SSL & TLS / OpenSSL →