Responses
The Reply Letter
When the server gets a request, it needs to send back a response. Think of it as the reply letter -- it tells the browser what happened and includes the content it asked for.
The Three Parts
1. The Status Line
The status code is a number that says what happened:
| Code | Meaning | Analogy |
|---|---|---|
| 200 | OK -- here's your page | "Found it! Here you go." |
| 201 | Created -- made something new | "Done! I created it for you." |
| 301 | Moved -- it's at a new address | "They moved. Try this address." |
| 400 | Bad Request -- I don't understand | "Your letter doesn't make sense." |
| 404 | Not Found -- page doesn't exist | "No one lives at that address." |
| 405 | Method Not Allowed -- wrong verb | "You can visit but not deliver." |
| 500 | Server Error -- something broke | "Sorry, I dropped your letter." |
2. The Headers
Headers tell the browser what kind of content is coming (HTML? JSON? an image?) and how big it is.
3. The Body
The actual content -- the HTML page, the JSON data, or the image.
Text (like HTML) is turned into bytes using UTF-8 before it's sent. But a picture isn't text at all -- it's just a long list of numbers -- so PyWeb lets a body be raw bytes too, and sends them exactly as they are. Trying to read a PNG as text would be like trying to read a photo out loud.
Building Responses in PyWeb
from pyweb.response import html_response, json_response, text_response, not_found
# HTML page
resp = html_response("<h1>Hello!</h1>")
# JSON API response
resp = json_response({"name": "Pikachu", "power": 55})
# Plain text
resp = text_response("Just some text")
# 404 error page
resp = not_found("That page doesn't exist")
What We Test
- Responses serialize to valid HTTP bytes.
- Content-Length is calculated automatically.
- Helper functions set the right Content-Type.
- Status codes map to the right phrases.
Next Up
We can read letters and write replies. But how does the server know which reply to send? Head to Routing.