Skip to main content

Request & File

The Request object is passed to every handler as the first argument. File represents an uploaded file from a multipart form.

Request

Properties

PropertyTypeDescription
methodstrHTTP method (GET, POST, ...)
uristrFull URI including the query string
headersdict[str, str]HTTP headers
datastr | NoneRaw body content, when present
formdict[str, str]Form fields for application/x-www-form-urlencoded bodies
filesdict[str, File]Uploaded files keyed by field name
app_dataAnyApplication-wide data set with HttpServer.app_data()
querydict[str, str]Parsed query string parameters
@get("/debug")
def debug(request):
return {
"method": request.method,
"uri": request.uri,
"user_agent": request.headers.get("user-agent"),
"query": request.query,
}

Methods

json

json() -> dict

Parses the request body as JSON. Raises an exception when the body is missing or not valid JSON.

@post("/api/data")
def create_data(request):
data = request.json()
return {"received": data}
get_cookie(name: str) -> str | None

Returns the value of the named cookie, or None if it is not present.

theme = request.get_cookie("theme") or "light"

Dynamic attributes

Requests support arbitrary attribute assignment, which is how middleware passes data to handlers:

def auth_middleware(request, next, **kw):
request.user_name = "John Doe"
return next(request, **kw)

File

Properties

PropertyTypeDescription
namestrOriginal file name
content_typestrUploaded file MIME type
contentbytesFull file content

save

save(path: str) -> None

Writes the uploaded file to path.

@post("/upload")
def upload(request):
image = request.files["profile_image"]
image.save(f"uploads/{image.name}")
return {"filename": image.name}

Example: full request handling

from oxapy import post


@post("/submit")
def submit(request):
result = {
"json": request.json(),
"form": dict(request.form),
"files": {
name: {"name": f.name, "size": len(f.content)}
for name, f in request.files.items()
},
}
return result