Sufast/Documentation
Response Object
Response Object Reference
Complete guide to creating and customizing HTTP responses in Sufast applications.
Response Types
Different ways to return responses from your endpoints
| Return Type | Content-Type | Status Code | Description |
|---|---|---|---|
| dict | application/json | 200 | JSON response (automatic) |
| list | application/json | 200 | JSON array response |
| str | text/plain | 200 | Plain text response |
| bytes | application/octet-stream | 200 | Binary data |
| Response | Custom | Custom | Full control over response |
| tuple | Varies | Custom | (content, status_code) |
Basic Response Types
Response Typespython
from sufast import App, Response
app = App()
# JSON response (automatic)
@app.get("/users")
def get_users():
return {
"users": [
{"id": 1, "name": "Alice"},
{"id": 2, "name": "Bob"}
],
"total": 2
}
# JSON array response
@app.get("/user-names")
def get_user_names():
return ["Alice", "Bob", "Charlie"]
# Plain text response
@app.get("/health")
def health_check():
return "OK"
# Binary response
@app.get("/image")
def get_image():
# Return binary image data
with open("image.png", "rb") as f:
return f.read()
# HTML response
@app.get("/page")
def get_page():
html = """
<!DOCTYPE html>
<html>
<head><title>Sufast Page</title></head>
<body><h1>Hello from Sufast!</h1></body>
</html>
"""
return Response(
content=html,
headers={"Content-Type": "text/html"}
)
# Custom status code with tuple
@app.post("/users")
def create_user():
user = {"id": 123, "name": "New User"}
return user, 201 # Created
@app.get("/not-found")
def not_found():
return {"error": "Resource not found"}, 404