Expand description
multipart/form-data request bodies (file uploads and mixed forms)
use ullun::prelude::*;
async fn upload(req: Request) -> Result<Response> {
let form = req.multipart()?;
let title = form.text("title").unwrap_or("untitled");
let Some(file) = form.file("avatar") else {
return Err(Error::bad_request("avatar is required"));
};
// never use the client's file name as a path: sanitize it, and store
// the bytes under a name you generated
let name = file.safe_filename().unwrap_or_else(|| "upload".into());
Ok(Response::json(serde_json::json!({ "title": title, "name": name, "bytes": file.data.len() })))
}The whole body is buffered, so it is bounded by the request body limit:
raise it for upload routes with App::body_limit / RouteGroup::max_body_size
and keep it modest. Parsing itself is limited by MultipartLimits.
Structs§
- Multipart
- The parts of a
multipart/form-databody, in order - Multipart
Limits - Bounds against hostile bodies
- Part
- One field or file of the form