Struct actix_web::web::JsonConfig [−][src]
pub struct JsonConfig { /* fields omitted */ }
Expand description
Json extractor configuration
Example
use actix_web::{error, web, App, FromRequest, HttpResponse};
use serde_derive::Deserialize;
#[derive(Deserialize)]
struct Info {
username: String,
}
/// deserialize `Info` from request's body, max payload size is 4kb
async fn index(info: web::Json<Info>) -> String {
format!("Welcome {}!", info.username)
}
fn main() {
let app = App::new().service(
web::resource("/index.html")
.app_data(
// 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))
);
}
Implementations
Change max size of payload. By default max size is 32Kb
pub fn error_handler<F>(self, f: F) -> Self where
F: Fn(JsonPayloadError, &HttpRequest) -> Error + Send + Sync + 'static,
pub fn error_handler<F>(self, f: F) -> Self where
F: Fn(JsonPayloadError, &HttpRequest) -> Error + Send + Sync + 'static,
Set custom error handler
Trait Implementations
Auto Trait Implementations
impl !RefUnwindSafe for JsonConfig
impl Send for JsonConfig
impl Sync for JsonConfig
impl Unpin for JsonConfig
impl !UnwindSafe for JsonConfig
Blanket Implementations
Mutably borrows from an owned value. Read more
Instruments this type with the provided Span
, returning an
Instrumented
wrapper. Read more
pub fn vzip(self) -> V
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more