小编Cot*_*ods的帖子

如何在中间件和处理程序中读取Iron请求?

我正在研究Rust中的一个小API,我不知道如何Request在两个地方访问Iron.

Authentication中间件读取Request一个令牌,一旦与实际路线试图如果路径是允许(目前还没有检查)看了一遍.这给了我一个EOF错误,因为已经读取了请求.

我似乎无法轻易克隆请求,我相信它必须是可变的才能阅读正文.

extern crate iron;
extern crate router;
extern crate rustc_serialize;

use iron::prelude::*;
use iron::{BeforeMiddleware, status};
use router::Router;
use rustc_serialize::json;
use rustc_serialize::json::Json;
use std::io::Read;

#[derive(RustcEncodable, RustcDecodable)]
struct Greeting {
    msg: String
}

struct Authentication;

fn main() {
    let mut request_body = String::new();

    impl BeforeMiddleware for Authentication {
        fn before(&self, request: &mut Request) -> IronResult<()> {
            let mut payload = String::new();
            request.body.read_to_string(&mut payload).unwrap();
            let json = Json::from_str(&payload).unwrap();

            println!("json: {}", json);

            let token = json.as_object() …
Run Code Online (Sandbox Code Playgroud)

middleware request iron rust

4
推荐指数
1
解决办法
1168
查看次数

标签 统计

iron ×1

middleware ×1

request ×1

rust ×1