如何正确拉出请求参数的值?

How do I correctly pull out the value of the request parameter?

前端有一个请求码:

return axios.get("http://127.0.0.1:8088/mike" , {
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded',
  },
  params: {
    foo: 'perfectGoods'
  }
})
.then(response => {
  console.log(JSON.stringify(response));
})
.catch(function (error) {
  console.log(error);
});

后台收到数据如下:

use actix_cors::Cors;
use actix_web::{http, web, App, HttpRequest, HttpResponse, HttpServer, Result};
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize)]
struct MyObj {
    name: String,
}

async fn index(obj: web::Path<MyObj>) -> Result<HttpResponse> {
    Ok(HttpResponse::Ok().json(MyObj {
        name: obj.name.to_string(),
    }))
}


#[actix_rt::main]
async fn main() -> std::io::Result<()> {

    use actix_web::{App, HttpServer};

    HttpServer::new(|| App::new()
        .wrap(
            Cors::new() // <- Construct CORS middleware builder
              .allowed_origin("http://localhost:3000")
              .allowed_methods(vec!["GET", "POST"])
              .allowed_headers(vec![http::header::AUTHORIZATION, http::header::ACCEPT])
              .allowed_header(http::header::CONTENT_TYPE)
              .max_age(3600)
              .finish())
        .service(
            web::resource("")
              .route(web::get().to(index))
              .route(web::head().to(|| HttpResponse::MethodNotAllowed())
        ))
        .route(r"{name}", web::get().to(index)))
        .bind("127.0.0.1:8088")?
        .run()
        .await
}



问题:
如何在后端提取从前端传递的 foo: perfectGoods 值?

我尝试尝试使用本手册中的不同示例:
https://actix.rs/docs/extractors/

但无法借助它完成必要的任务。
(有些例子给出了编译器错误 - 有些我并不完全清楚它们的工作原理。)
我想看看获得这个值的最原始的方式,从 ..

开始

前端代码有错误。 HTTP Content-Type: application/x-www-form-urlencoded 用于表单 post 未获取。

前端使用 Post 请求:

// post request
axios.post("http://127.0.0.1:8088/mike" , {
  foo: 'perfectGoods'
})
.then(response => {
  console.log(response.data);
})
.catch(function (error) {
  console.log(error);
});

或者使用 Get 请求的前端:

// get request
return axios.get("http://127.0.0.1:8088/mike" , {
  params: {
    foo: 'perfectGoods'
  }
})
.then(response => {
  console.log(JSON.stringify(response));
})
.catch(function (error) {
  console.log(error);
});

然后使用 web::Form 提取器从 post 请求中获取 foo 参数,并使用 web::Query 从 get 请求中获取 foo 参数。

这是支持两个 post/get 请求的后端。使用最新的 actix-web 3.0.0 版本进行测试。

Cargo.toml

[package]
name = "web_form"
version = "0.1.0"
edition = "2018"

[dependencies]
actix-web = "3.0.1"
actix-cors = "0.3.0"
serde = "1.0.116"
actix-rt = "1.1.1"
env_logger = "0.7.1"

src/main.rs

use actix_cors::Cors;
use actix_web::{http, web, get, post, App, HttpResponse, HttpServer, Result};
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize)]
struct MyObj {
    name: String,
}

#[derive(Serialize, Deserialize, Clone)]
struct MyParams {
    foo: Option<String>,
}

#[derive(Serialize, Deserialize)]
struct MyResponseObj {
    name: String,
    params: MyParams,
}

#[get("/{name}")]
async fn index_get(path: web::Path<MyObj>, params: web::Query<MyParams>) -> Result<HttpResponse> {
    Ok(HttpResponse::Ok().json(MyResponseObj {
        name: path.name.to_string(),
        params: params.clone(),
    }))
}

#[post("/{name}")]
async fn index_post(path: web::Path<MyObj>, params: web::Json<MyParams>) -> Result<HttpResponse> {
    Ok(HttpResponse::Ok().json(MyResponseObj {
        name: path.name.to_string(),
        params: params.clone(),
    }))
}

#[actix_rt::main]
async fn main() -> std::io::Result<()> {

    HttpServer::new(|| App::new()
        .wrap(
            Cors::new() // <- Construct CORS middleware builder
              .allowed_origin("http://localhost:3000")
              .allowed_methods(vec!["GET", "POST"])
              .allowed_headers(vec![http::header::AUTHORIZATION, http::header::ACCEPT])
              .allowed_header(http::header::CONTENT_TYPE)
              .max_age(3600)
              .finish())
        .service(index_get)
        .service(index_post)
    )
        .bind("127.0.0.1:8088")?
        .run()
        .await
}