From c273b7ac3fba82e5b740af255a0155f3e5511252 Mon Sep 17 00:00:00 2001 From: Nikolay Kim <fafhrd91@gmail.com> Date: Wed, 4 Apr 2018 08:08:31 -0700 Subject: [PATCH] update json example --- examples/json/src/main.rs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/examples/json/src/main.rs b/examples/json/src/main.rs index 34730366..73863eef 100644 --- a/examples/json/src/main.rs +++ b/examples/json/src/main.rs @@ -21,7 +21,7 @@ struct MyObj { number: i32, } -/// This handler uses `HttpRequest::json()` for loading serde json object. +/// This handler uses `HttpRequest::json()` for loading json object. fn index(req: HttpRequest) -> Box<Future<Item=HttpResponse, Error=Error>> { req.json() .from_err() // convert all errors into `Error` @@ -32,7 +32,7 @@ fn index(req: HttpRequest) -> Box<Future<Item=HttpResponse, Error=Error>> { .responder() } -/// This handler uses `With` helper for loading serde json object. +/// This handler uses json extractor fn extract_item(item: Json<MyObj>) -> HttpResponse { println!("model: {:?}", &item); HttpResponse::Ok().json(item.0) // <- send response @@ -40,7 +40,7 @@ fn extract_item(item: Json<MyObj>) -> HttpResponse { const MAX_SIZE: usize = 262_144; // max payload size is 256k -/// This handler manually load request payload and parse serde json +/// This handler manually load request payload and parse json object fn index_manual(req: HttpRequest) -> Box<Future<Item=HttpResponse, Error=Error>> { // HttpRequest is stream of Bytes objects req @@ -86,15 +86,18 @@ fn index_mjsonrust(req: HttpRequest) -> Box<Future<Item=HttpResponse, Error=Erro fn main() { ::std::env::set_var("RUST_LOG", "actix_web=info"); - let _ = env_logger::init(); + env_logger::init(); let sys = actix::System::new("json-example"); let _ = server::new(|| { App::new() // enable logger .middleware(middleware::Logger::default()) - .resource("/extractor/{name}/{number}/", - |r| r.method(http::Method::GET).with(extract_item)) + .resource("/extractor", |r| { + r.method(http::Method::POST) + .with(extract_item) + .limit(4096); // <- limit size of the payload + }) .resource("/manual", |r| r.method(http::Method::POST).f(index_manual)) .resource("/mjsonrust", |r| r.method(http::Method::POST).f(index_mjsonrust)) .resource("/", |r| r.method(http::Method::POST).f(index))})