-1

ハイパー 0.11.2 を使用して POST された JSON を読み込もうとしています。「Reached」が出力された後、何も起こっていません。

fn call(&self, req: hyper::server::Request) -> Self::Future {
    let mut response: Response = Response::new();

    match (req.method(), req.path()) {
        (&Method::Post, "/assests") => {
             println!("Reached ... "); //POST: 200 OK
            //let (method, uri, _version, head 
            let mut res: Response = Response::new();
            if let Some(len) = req.headers().get::<ContentLength>() {
                res.headers_mut().set(len.clone());
            }

            println!("Reached xxx {:?}", req.body());
            res.with_body("req.body()");
        }
        _ => {
            response.set_status(StatusCode::NotFound);
        }
    };

    futures::future::ok(response)
}

出力:

Reached ...
Reached xxx Body(Body { [stream of values] })
4

3 に答える 3

-1

https://hyper.rs/guides/server/handle_post/#handling-json-and-other-data-typesによると

コードは次のとおりです。

extern crate futures;
extern crate hyper;
extern crate serde_json;

use futures::Future;
use futures::{Stream};

use hyper::{Get, Post, StatusCode};
use hyper::header::{ContentLength};
use hyper::server::{Http, Service, Request, Response};

static INDEX: &'static [u8] = b"Try POST /echo";

struct Echo;

impl Service for Echo {
    type Request = Request;
    type Response = Response;
    type Error = hyper::Error;
    type Future = Box<Future<Item = Self::Response, Error = Self::Error>>;

    fn call(&self, req: Request) -> Self::Future {
        match (req.method(), req.path()) {
            (&Get, "/") | (&Get, "/echo") => {
                Box::new(futures::future::ok(
                    Response::new()
                        .with_header(ContentLength(INDEX.len() as u64))
                        .with_body(INDEX)))
            },
            (&Post, "/echo") => {
        Box::new(req.body().concat2().map(|b| {
                    let bad_request: &[u8] = b"Missing field";
                    let json: serde_json::Value =
                        if let Ok(j) = serde_json::from_slice(b.as_ref()) {
                        j
                        } else {
                            return Response::new()
                                .with_status(StatusCode::BadRequest)
                                .with_header(ContentLength(bad_request.len() as u64))
                                .with_body(bad_request);
                    };

                    // use json as you like

                    let body = serde_json::to_string(&json).unwrap();
                    Response::new()
                        .with_header(ContentLength(body.len() as u64))
                        .with_body(body)
                }))
            },
            _ => {
                Box::new(futures::future::ok(
                    Response::new().with_status(StatusCode::NotFound)))
            }
        }
    }
}


fn main() {
    let addr = "127.0.0.1:1337".parse().unwrap();

    let server = Http::new().bind(&addr, || Ok(Echo)).unwrap();
    println!("Listening on http://{} with 1 thread.", server.local_addr().unwrap());
    server.run().unwrap();
}
于 2018-01-12T05:01:25.583 に答える