There is a request code from the frontend:
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);
});
On the backend, data is received as follows:
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
}
Question:
How, on the backend, I can pull out the foo: perfectGoods value passed from the frontend?
I tried to try different examples from this manual:
https://actix.rs/docs/extractors/
But it was not possible to accomplish the necessary task with the help of it.
(Some examples give a compiler error - some are not completely clear to me on what principle they function.)
I would like to see the most primitive way of getting this value, to begin with ..