mirror of
https://github.com/actix/actix-website
synced 2025-06-29 16:24:58 +02:00
update extractors
This commit is contained in:
@ -19,19 +19,17 @@ async fn main() -> std::io::Result<()> {
|
||||
HttpServer::new(|| {
|
||||
App::new().service(
|
||||
web::resource("/")
|
||||
.data(
|
||||
// change json extractor configuration
|
||||
web::Json::<Info>::configure(|cfg| {
|
||||
cfg.limit(4096).error_handler(|err, _req| {
|
||||
// <- create custom error response
|
||||
error::InternalError::from_response(
|
||||
err,
|
||||
HttpResponse::Conflict().finish(),
|
||||
)
|
||||
.into()
|
||||
})
|
||||
}),
|
||||
)
|
||||
// change json extractor configuration
|
||||
.app_data(web::Json::<Info>::configure(|cfg| {
|
||||
cfg.limit(4096).error_handler(|err, _req| {
|
||||
// create custom error response
|
||||
error::InternalError::from_response(
|
||||
err,
|
||||
HttpResponse::Conflict().finish(),
|
||||
)
|
||||
.into()
|
||||
})
|
||||
}))
|
||||
.route(web::post().to(index)),
|
||||
)
|
||||
})
|
||||
|
@ -7,7 +7,9 @@ struct Info {
|
||||
username: String,
|
||||
}
|
||||
|
||||
async fn index((path, query): (web::Path<(u32, String)>, web::Query<Info>)) -> String {
|
||||
async fn index(
|
||||
(path, query): (web::Path<(u32, String)>, web::Query<Info>),
|
||||
) -> String {
|
||||
format!(
|
||||
"Welcome {}, friend {}, userid {}!",
|
||||
query.username, path.1, path.0
|
||||
|
@ -2,7 +2,8 @@ use actix_web::{web, HttpRequest, Result};
|
||||
|
||||
// <path-three>
|
||||
async fn index(req: HttpRequest) -> Result<String> {
|
||||
let name: String = req.match_info().get("friend").unwrap().parse().unwrap();
|
||||
let name: String =
|
||||
req.match_info().get("friend").unwrap().parse().unwrap();
|
||||
let userid: i32 = req.match_info().query("userid").parse().unwrap();
|
||||
|
||||
Ok(format!("Welcome {}, userid {}!", name, userid))
|
||||
|
Reference in New Issue
Block a user