-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathbodyparser.rs
55 lines (47 loc) · 1.68 KB
/
bodyparser.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
extern crate iron;
extern crate bodyparser;
extern crate persistent;
extern crate serde;
#[macro_use]
extern crate serde_derive;
use persistent::Read;
use iron::status;
use iron::prelude::*;
#[derive(Deserialize, Debug, Clone)]
struct MyStructure {
a: String,
b: Option<String>,
}
fn log_body(req: &mut Request) -> IronResult<Response> {
let body = req.get::<bodyparser::Raw>();
match body {
Ok(Some(body)) => println!("Read body:\n{}", body),
Ok(None) => println!("No body"),
Err(err) => println!("Error: {:?}", err)
}
let json_body = req.get::<bodyparser::Json>();
match json_body {
Ok(Some(json_body)) => println!("Parsed body:\n{:?}", json_body),
Ok(None) => println!("No body"),
Err(err) => println!("Error: {:?}", err)
}
let struct_body = req.get::<bodyparser::Struct<MyStructure>>();
match struct_body {
Ok(Some(struct_body)) => println!("Parsed body:\n{:?}", struct_body),
Ok(None) => println!("No body"),
Err(err) => println!("Error: {:?}", err)
}
Ok(Response::with(status::Ok))
}
const MAX_BODY_LENGTH: usize = 1024 * 1024 * 10;
// While the example is running, try the following curl commands and see how they are
// logged by the Rust server process:
//
// `curl -i "localhost:3000/" -H "application/json" -d '{"name":"jason","age":"2"}'`
// `curl -i "localhost:3000/" -H "application/json" -d '{"a":"jason","b":"2"}'`
// `curl -i "localhost:3000/" -H "application/json" -d '{"a":"jason"}'`
fn main() {
let mut chain = Chain::new(log_body);
chain.link_before(Read::<bodyparser::MaxBodyLength>::one(MAX_BODY_LENGTH));
Iron::new(chain).http("localhost:3000").unwrap();
}