Response API¶
The Response class represents an HTTP response. It provides factory class methods for common content types and allows setting headers, status codes, and cookies.
Importing¶
Factory Methods¶
Response.json(data, status=200)¶
Create a JSON response. The data is serialized using Cello's Rust SIMD JSON engine.
| Parameter | Type | Default | Description |
|---|---|---|---|
data | dict or list | Required | Data to serialize as JSON |
status | int | 200 | HTTP status code |
Returns: Response
Response.text(body, status=200)¶
Create a plain text response with Content-Type: text/plain.
| Parameter | Type | Default | Description |
|---|---|---|---|
body | str | Required | Response body text |
status | int | 200 | HTTP status code |
Returns: Response
Response.html(body, status=200)¶
Create an HTML response with Content-Type: text/html.
| Parameter | Type | Default | Description |
|---|---|---|---|
body | str | Required | HTML content |
status | int | 200 | HTTP status code |
Returns: Response
Response.binary(data, content_type="application/octet-stream", status=200)¶
Create a binary response for file downloads or arbitrary byte data.
Response.binary(pdf_bytes, content_type="application/pdf")
Response.binary(image_data, content_type="image/png")
| Parameter | Type | Default | Description |
|---|---|---|---|
data | bytes | Required | Binary content |
content_type | str | "application/octet-stream" | MIME type |
status | int | 200 | HTTP status code |
Returns: Response
Response.redirect(url, status=302)¶
Create a redirect response.
| Parameter | Type | Default | Description |
|---|---|---|---|
url | str | Required | Target URL |
status | int | 302 | Redirect status code (301, 302, 303, 307, 308) |
Returns: Response
Response.xml(body, status=200)¶
Create an XML response with Content-Type: application/xml.
| Parameter | Type | Default | Description |
|---|---|---|---|
body | str | Required | XML content |
status | int | 200 | HTTP status code |
Returns: Response
Instance Methods¶
response.set_header(name, value)¶
Set a response header. If the header already exists, it is overwritten.
resp = Response.json({"ok": True})
resp.set_header("X-Custom", "value")
resp.set_header("Cache-Control", "no-cache")
| Parameter | Type | Description |
|---|---|---|
name | str | Header name |
value | str | Header value |
response.set_cookie(name, value, **options)¶
Set a cookie on the response.
resp = Response.json({"logged_in": True})
resp.set_cookie("session_id", "abc123", httponly=True, secure=True, max_age=3600)
| Parameter | Type | Description |
|---|---|---|
name | str | Cookie name |
value | str | Cookie value |
max_age | int | Lifetime in seconds |
path | str | Cookie path (default: "/") |
domain | str | Cookie domain |
secure | bool | Require HTTPS |
httponly | bool | Prevent JavaScript access |
samesite | str | "Strict", "Lax", or "None" |
Returning Responses from Handlers¶
Returning a dict (fastest)¶
Returning a Response object¶
Setting custom headers¶
@app.get("/data")
def get_data(request):
resp = Response.json({"data": "value"})
resp.set_header("X-Request-ID", "abc-123")
return resp
Status Code Helpers¶
Cello does not provide named status code constants. Use integer literals directly.
Response.json(data, status=200) # OK
Response.json(data, status=201) # Created
Response.json(data, status=204) # No Content
Response.json(data, status=400) # Bad Request
Response.json(data, status=401) # Unauthorized
Response.json(data, status=403) # Forbidden
Response.json(data, status=404) # Not Found
Response.json(data, status=422) # Unprocessable Entity
Response.json(data, status=429) # Too Many Requests
Response.json(data, status=500) # Internal Server Error
Summary¶
| Method | Content-Type | Use Case |
|---|---|---|
Response.json() | application/json | API responses |
Response.text() | text/plain | Plain text |
Response.html() | text/html | HTML pages |
Response.binary() | Custom | File downloads, images |
Response.redirect() | N/A | URL redirects |
Response.xml() | application/xml | XML APIs |