Expand description
Returns a Filter
that matches any request and extracts a Future
of a
JSON-decoded body.
Warning
This does not have a default size limit, it would be wise to use one to prevent a overly large request from using too much memory.
Example
use std::collections::HashMap;
use warp::Filter;
let route = warp::body::content_length_limit(1024 * 32)
.and(warp::body::json())
.map(|simple_map: HashMap<String, String>| {
"Got a JSON body!"
});
Examples found in repository?
More examples
examples/body.rs (line 23)
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
async fn main() {
pretty_env_logger::init();
// POST /employees/:rate {"name":"Sean","rate":2}
let promote = warp::post()
.and(warp::path("employees"))
.and(warp::path::param::<u32>())
// Only accept bodies smaller than 16kb...
.and(warp::body::content_length_limit(1024 * 16))
.and(warp::body::json())
.map(|rate, mut employee: Employee| {
employee.rate = rate;
warp::reply::json(&employee)
});
warp::serve(promote).run(([127, 0, 0, 1], 3030)).await
}
examples/rejections.rs (line 28)
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
async fn main() {
let math = warp::path!("math" / u16);
let div_with_header = math
.and(warp::get())
.and(div_by())
.map(|num: u16, denom: NonZeroU16| {
warp::reply::json(&Math {
op: format!("{} / {}", num, denom),
output: num / denom.get(),
})
});
let div_with_body =
math.and(warp::post())
.and(warp::body::json())
.map(|num: u16, body: DenomRequest| {
warp::reply::json(&Math {
op: format!("{} / {}", num, body.denom),
output: num / body.denom.get(),
})
});
let routes = div_with_header.or(div_with_body).recover(handle_rejection);
warp::serve(routes).run(([127, 0, 0, 1], 3030)).await;
}