I am building a little webapp in Actix-web, but I can't find any example of getting parameters out of a POST request in Actix-web anywhere.
Searching their excellent examples repo only gives a couple of (to me) meaningful examples, but they both deal with JSON and not form data.
I also found this page, which I suspect holds the answer; but to a beginner this isn't much help.
I imagine it should look something like:
<form method="POST">
<input type="password" name="password">
<button type="submit">Login</button>
</form>
and
fn main() {
// ...
App::with_state(AppState { db: pool.clone() })
.middleware(IdentityService::new(
CookieIdentityPolicy::new(&[0; 32])
.name("auth-cookie")
.secure(true),
))
.resource("/login", |r| {
r.method(http::Method::GET).with(login);
r.method(http::Method::POST).with(perform_login) // help!
})
}
struct LoginParams {
password: String,
}
fn perform_login(mut req: HttpRequest<AppState>, params: LoginParams) -> HttpResponse {
if params.password == "abc123" {
req.remember("logged-in".to_owned());
// redirect to home
};
// show "wrong password" error
}
You can use an extractor by:
Form as Nikolay said, with the type parameter of your struct.If you take a look at simple example in linked documentation you'll see how describe such handler.
Here is a bit more complete one:
#[derive(Deserialize)]
struct AddHook {
id: u64,
title: String,
version: Option<String>,
code: Option<String>
}
fn remove_hook_del((query, state): (Form<AddHook>, State<AppState>)) -> FutureHttpResponse {
let query = query.into_inner();
let AddHook {id, title, version, code} = query;
//Do something with your data
}
App::with_state(AppState::new()).resource("/remove_hook", |res| {
res.method(Method::GET).with(remove_hook_get);
res.method(Method::DELETE).with(remove_hook_del);
res.route().f(not_allowed);
})
This is more or less a complete example for the current master branch of actix-web. I also made it with state to show how you can use multiple arguments in your handler
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With