mirror of
https://github.com/actix/actix-extras.git
synced 2024-11-30 18:34:36 +01:00
update json example
This commit is contained in:
parent
df21892b5b
commit
c273b7ac3f
@ -21,7 +21,7 @@ struct MyObj {
|
|||||||
number: i32,
|
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>> {
|
fn index(req: HttpRequest) -> Box<Future<Item=HttpResponse, Error=Error>> {
|
||||||
req.json()
|
req.json()
|
||||||
.from_err() // convert all errors into `Error`
|
.from_err() // convert all errors into `Error`
|
||||||
@ -32,7 +32,7 @@ fn index(req: HttpRequest) -> Box<Future<Item=HttpResponse, Error=Error>> {
|
|||||||
.responder()
|
.responder()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// This handler uses `With` helper for loading serde json object.
|
/// This handler uses json extractor
|
||||||
fn extract_item(item: Json<MyObj>) -> HttpResponse {
|
fn extract_item(item: Json<MyObj>) -> HttpResponse {
|
||||||
println!("model: {:?}", &item);
|
println!("model: {:?}", &item);
|
||||||
HttpResponse::Ok().json(item.0) // <- send response
|
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
|
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>> {
|
fn index_manual(req: HttpRequest) -> Box<Future<Item=HttpResponse, Error=Error>> {
|
||||||
// HttpRequest is stream of Bytes objects
|
// HttpRequest is stream of Bytes objects
|
||||||
req
|
req
|
||||||
@ -86,15 +86,18 @@ fn index_mjsonrust(req: HttpRequest) -> Box<Future<Item=HttpResponse, Error=Erro
|
|||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
::std::env::set_var("RUST_LOG", "actix_web=info");
|
::std::env::set_var("RUST_LOG", "actix_web=info");
|
||||||
let _ = env_logger::init();
|
env_logger::init();
|
||||||
let sys = actix::System::new("json-example");
|
let sys = actix::System::new("json-example");
|
||||||
|
|
||||||
let _ = server::new(|| {
|
let _ = server::new(|| {
|
||||||
App::new()
|
App::new()
|
||||||
// enable logger
|
// enable logger
|
||||||
.middleware(middleware::Logger::default())
|
.middleware(middleware::Logger::default())
|
||||||
.resource("/extractor/{name}/{number}/",
|
.resource("/extractor", |r| {
|
||||||
|r| r.method(http::Method::GET).with(extract_item))
|
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("/manual", |r| r.method(http::Method::POST).f(index_manual))
|
||||||
.resource("/mjsonrust", |r| r.method(http::Method::POST).f(index_mjsonrust))
|
.resource("/mjsonrust", |r| r.method(http::Method::POST).f(index_mjsonrust))
|
||||||
.resource("/", |r| r.method(http::Method::POST).f(index))})
|
.resource("/", |r| r.method(http::Method::POST).f(index))})
|
||||||
|
Loading…
Reference in New Issue
Block a user