1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-30 18:44:35 +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") /// web::resource("/index.html")
/// // change `Form` extractor configuration /// // change `Form` extractor configuration
/// .app_data( /// .app_data(
/// web::Form::<FormData>::configure(|cfg| cfg.limit(4097)) /// web::FormConfig::default().limit(4097)
/// ) /// )
/// .route(web::get().to(index)) /// .route(web::get().to(index))
/// ); /// );

View File

@ -209,10 +209,10 @@ where
/// Json extractor configuration /// Json extractor configuration
/// ///
/// # Examples /// # Example
/// ///
/// ```rust /// ```rust
/// use actix_web::{error, web, App, FromRequest, HttpRequest, HttpResponse}; /// use actix_web::{error, web, App, FromRequest, HttpResponse};
/// use serde_derive::Deserialize; /// use serde_derive::Deserialize;
/// ///
/// #[derive(Deserialize)] /// #[derive(Deserialize)]
@ -225,35 +225,26 @@ where
/// format!("Welcome {}!", info.username) /// 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() { /// fn main() {
/// let app = App::new().service( /// let app = App::new().service(
/// web::resource("/index.html") /// web::resource("/index.html")
/// .app_data( /// .app_data(
/// // change json extractor configuration /// // Json extractor configuration for this resource.
/// web::Json::<Info>::configure(|cfg| { /// web::JsonConfig::default()
/// cfg.limit(4096) /// .limit(4096) // Limit request payload size
/// .content_type(|mime| { // <- accept text/plain content type /// .content_type(|mime| { // <- accept text/plain content type
/// mime.type_() == mime::TEXT && mime.subtype() == mime::PLAIN /// mime.type_() == mime::TEXT && mime.subtype() == mime::PLAIN
/// }) /// })
/// .error_handler(json_error_handler) // Use our custom error response /// .error_handler(|err, req| { // <- create custom error response
/// })) /// error::InternalError::from_response(
/// err, HttpResponse::Conflict().finish()).into()
/// })
/// )
/// .route(web::post().to(index)) /// .route(web::post().to(index))
/// ); /// );
/// } /// }
/// ``` /// ```
///
#[derive(Clone)] #[derive(Clone)]
pub struct JsonConfig { pub struct JsonConfig {
limit: usize, limit: usize,

View File

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