π¨ Handling Request Inputs β
To handle JSON data in a request, you can use the json property of the Request object. This property asynchronously parses the request body as JSON and returns it as a dictionary.
from nexios import NexiosApp
app = NexiosApp()
@app.post("/submit")
async def submit_data(req, res):
data = await req.json
return {"received": data}π‘Tip
Nexios only process application/json content types for JSON data.
π Form Data β
Nexios provides built-in support for parsing form data, including multipart/form-data and application/x-www-form-urlencoded. You can access the parsed form data using the form_data property of the Request object.
Example
@app.post("/submit-form")
async def submit_form(req, res):
form_data = await req.form_data
return {"received": form_data}π Handling File Uploads β
When handling file uploads, the form_data property also provides access to the uploaded files. You can iterate over the form data to extract files.
@app.post("/upload")
async def upload_file(req, res):
files = await req.files
for name, file in files.items():
# Process the uploaded file
pass
return {"status": "files received"}π Handling Streaming Request Data β
For large payloads or real-time data, you might need to handle streaming request data. Nexios supports streaming data using the req.stream property.
@app.post("/stream")
async def stream_data(req, res):
data = b""
async for chunk in req.stream():
data += chunk
print(data.decode())
return {"status": "stream received"}β Validating Inputs β
Nexios integrates with Pydantic for input validation. You can define Pydantic models to validate and parse request data.
Example
from pydantic import BaseModel, EmailStr, ValidationError
class UserSchema(BaseModel):
name: str
email: EmailStr
@app.post("/create-user")
async def create_user(req, res):
try:
user_data = await req.json
user = UserSchema(**user_data)
return res.json({"user": user.dict()})
except ValidationError as e:
return res.json({"error": e.errors()}, status_code=422)