mirror of
https://github.com/fafhrd91/actix-web
synced 2024-11-27 17:52:56 +01:00
Improve json, form and query extractor config docs (#1661)
This commit is contained in:
parent
7787638f26
commit
22089aff87
@ -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))
|
||||
/// );
|
||||
|
@ -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)
|
||||
/// // 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(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))
|
||||
/// );
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
#[derive(Clone)]
|
||||
pub struct JsonConfig {
|
||||
limit: usize,
|
||||
|
@ -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))
|
||||
/// );
|
||||
/// }
|
||||
|
Loading…
Reference in New Issue
Block a user