HTTP at Design Level
HTTP is the set of rules clients and servers use to send requests and responses. Design work treats each exchange as one request in and one response out.
A request and its response as data
This demo models the messages as plain data, with no real network traffic. The request carries a method and a path, and the response carries a status.
request = {"method": "GET", "path": "/photos"}
response = {"status": 200, "body": "photo page"}
print("request:", request["method"], request["path"])
print("response:", response["status"], response["body"])A missing page gives 404
The server holds a fixed set of pages. A path outside that set gets a 404 response.
pages = {"/photos": "photo page", "/about": "about page"}
def handle(path):
if path not in pages:
return 404, "no such page"
return 200, pages[path]
for path in ["/photos", "/settings"]:
status, body = handle(path)
print("GET", path, "->", status, body)A broken part gives 500
One path leads to a part of the server that is broken. Requests for that path get a 500 response.
pages = {"/photos": "photo page", "/report": "report page"}
broken = {"/report"}
def handle(path):
if path in broken:
return 500, "server error"
return 200, pages[path]
for path in ["/photos", "/report"]:
status, body = handle(path)
print("GET", path, "->", status, body)Example
pages = {"/photos": "photo page", "/about": "about page"}
broken = {"/report"}
def handle(method, path):
if path in broken:
return 500, "server error"
if path not in pages:
return 404, "no such page"
return 200, pages[path]
requests = [("GET", "/photos"), ("GET", "/about"), ("GET", "/settings"), ("GET", "/report")]
for method, path in requests:
status, body = handle(method, path)
print("request:", method, path, "-> response:", status, body)Key points
- HTTP request: the message a client sends under the HTTP rules.
- Method: the word in a request that says what the client wants done.
- Path: the part of a request that names what is wanted, such as /photos.
- Status: the number in a response that reports how the request went.
- 200: the status that means the request worked.
- 404: the status that means the server could not find the thing asked for.
- 500: the status that means something broke on the server side.
In this example, one handler answers four requests and prints each status. The statuses separate success, a missing page and a server-side break in one run.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
A response carries status 404. What does it mean?