Actix 2.0中如何从Request中获取Cookie

Sal*_*Aom 7 rust actix-web

我想从请求中获取cookie的值。我发现在 Actix 0.xx 中,cookie 的值可以通过调用获取

fn get_cookie(req: HttpRequest) {
    let cookie = req.cookie("name") <-- Here

    return HttpResponse::Ok()
        .body(
            format!("{}", cookie);
        )
}
Run Code Online (Sandbox Code Playgroud)

我对 Rust 和 Actix 还很陌生。目前我正在从声明的函数解析它,该函数获取HttpRequest.headers(). 我不确定是否有像 Actix 0.xx 中那样直接获取 cookie 的方法

pub fn get_cookie(req: HttpRequest, name: &str) -> String {
    let cookie: Vec<&str> = req
        .headers()
        .get("cookie")
        .unwrap()
        .to_str()
        .unwrap()
        .split("&")
        .collect();

    let auth_token: Vec<&str> = cookie
        .into_iter()
        .filter(|each| {
            let body: Vec<&str> = each.split("=").collect();

            body[0] == name
        })
        .collect();

    let cookie_part: Vec<&str> = auth_token[0].split("=").collect();

    cookie_part[1].to_owned() 
}
Run Code Online (Sandbox Code Playgroud)