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.
| Item | Syntax / Value | Description |
|---|---|---|
| GET | GET /path HTTP/1.1 | Retrieve resource — safe, idempotent, cacheable |
| POST | POST /path HTTP/1.1 | Submit data — creates resource, not idempotent |
| PUT | PUT /path HTTP/1.1 | Replace entire resource — idempotent |
| PATCH | PATCH /path HTTP/1.1 | Partial update — idempotent |
| DELETE | DELETE /path HTTP/1.1 | Remove resource — idempotent |
| HEAD | HEAD /path HTTP/1.1 | Like GET but no body — check existence/headers |
| OPTIONS | OPTIONS /path HTTP/1.1 | List supported methods — used in CORS preflight |
| 200 OK | 200 OK | Request succeeded |
| 201 Created | 201 Created | Resource created (POST/PUT) |
| 204 No Content | 204 No Content | Success, no body (DELETE) |
| 301 Moved Permanently | 301 Moved Permanently | Permanent redirect — update bookmarks |
| 302 Found | 302 Found | Temporary redirect |
| 304 Not Modified | 304 Not Modified | Cached version is current — use it |
| 400 Bad Request | 400 Bad Request | Malformed request syntax |
| 401 Unauthorized | 401 Unauthorized | Authentication required |
| 403 Forbidden | 403 Forbidden | Authenticated but not authorized |
| 404 Not Found | 404 Not Found | Resource does not exist |
| 405 Method Not Allowed | 405 Method Not Allowed | HTTP method not supported |
| 409 Conflict | 409 Conflict | State conflict — duplicate, version mismatch |
| 422 Unprocessable | 422 Unprocessable Entity | Validation error on submitted data |
| 429 Too Many Requests | 429 Too Many Requests | Rate limit exceeded |
| 500 Internal Server Error | 500 Internal Server Error | Server-side error |
| 502 Bad Gateway | 502 Bad Gateway | Upstream server returned invalid response |
| 503 Service Unavailable | 503 Service Unavailable | Server overloaded or down for maintenance |
| Content-Type | Content-Type: application/json | MIME type of request/response body |
| Authorization | Authorization: Bearer token123 | Auth token in request header |
| Cache-Control | Cache-Control: max-age=3600, public | Caching directive |
| CORS | Access-Control-Allow-Origin: * | Cross-Origin Resource Sharing header |
| HTTP/2 | Multiplexed streams, header compression, server push | Multiple requests over one TCP connection |
| HTTP/3 | QUIC protocol — UDP-based, 0-RTT connection | Faster than HTTP/2, especially on lossy networks |
# 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()