1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-27 09:42:57 +01:00

Improve json, form and query extractor config docs (#1661)

This commit is contained in:
Robert Gabriel Jakabosky 2020-09-10 22:40:20 +08:00 committed by GitHub
parent 7787638f26
commit 22089aff87
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 27 deletions

View File

@ -195,7 +195,7 @@ impl<T: Serialize> Responder for Form<T> {
/// web::resource("/index.html")
/// // change `Form` extractor configuration
/// .app_data(
/// web::Form::<FormData>::configure(|cfg| cfg.limit(4097))
/// web::FormConfig::default().limit(4097)
/// )
/// .route(web::get().to(index))
/// );

View File

@ -209,10 +209,10 @@ where
/// Json extractor configuration
///
/// # Examples
/// # Example
///
/// ```rust
/// use actix_web::{error, web, App, FromRequest, HttpRequest, HttpResponse};
/// use actix_web::{error, web, App, FromRequest, HttpResponse};
/// use serde_derive::Deserialize;
///
/// #[derive(Deserialize)]
@ -225,35 +225,26 @@ where
/// format!("Welcome {}!", info.username)
/// }
///
/// /// Return either a 400 or 415, and include the error message from serde
/// /// in the response body
/// fn json_error_handler(err: error::JsonPayloadError, _req: &HttpRequest) -> error::Error {
/// let detail = err.to_string();
/// let response = match &err {
/// error::JsonPayloadError::ContentType => {
/// HttpResponse::UnsupportedMediaType().content_type("text/plain").body(detail)
/// }
/// _ => HttpResponse::BadRequest().content_type("text/plain").body(detail),
/// };
/// error::InternalError::from_response(err, response).into()
/// }
///
/// fn main() {
/// let app = App::new().service(
/// web::resource("/index.html")
/// .app_data(
/// // change json extractor configuration
/// web::Json::<Info>::configure(|cfg| {
/// cfg.limit(4096)
/// .content_type(|mime| { // <- accept text/plain content type
/// mime.type_() == mime::TEXT && mime.subtype() == mime::PLAIN
/// })
/// .error_handler(json_error_handler) // Use our custom error response
/// }))
/// // Json extractor configuration for this resource.
/// web::JsonConfig::default()
/// .limit(4096) // Limit request payload size
/// .content_type(|mime| { // <- accept text/plain content type
/// mime.type_() == mime::TEXT && mime.subtype() == mime::PLAIN
/// })
/// .error_handler(|err, req| { // <- create custom error response
/// error::InternalError::from_response(
/// err, HttpResponse::Conflict().finish()).into()
/// })
/// )
/// .route(web::post().to(index))
/// );
/// }
/// ```
///
#[derive(Clone)]
pub struct JsonConfig {
limit: usize,

View File

@ -188,12 +188,12 @@ where
/// let app = App::new().service(
/// web::resource("/index.html").app_data(
/// // change query extractor configuration
/// web::Query::<Info>::configure(|cfg| {
/// cfg.error_handler(|err, req| { // <- create custom error response
/// web::QueryConfig::default()
/// .error_handler(|err, req| { // <- create custom error response
/// error::InternalError::from_response(
/// err, HttpResponse::Conflict().finish()).into()
/// })
/// }))
/// )
/// .route(web::post().to(index))
/// );
/// }