pub struct JsonConfig { /* private fields */ }
Expand description
Json extractor configuration
use actix_web::{error, web, App, FromRequest, HttpResponse};
use serde::Deserialize;
use actix_web_validator::{Json, JsonConfig};
use validator::Validate;
#[derive(Deserialize, Validate)]
struct Info {
#[validate(length(min = 3))]
username: String,
}
/// deserialize `Info` from request's body, max payload size is 4kb
async fn index(info: Json<Info>) -> String {
format!("Welcome {}!", info.username)
}
fn main() {
let json_config = JsonConfig::default().limit(4096)
.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()
});
let app = App::new().service(
web::resource("/index.html")
.app_data(json_config)
.route(web::post().to(index))
);
}
Implementations§
Source§impl JsonConfig
impl JsonConfig
Sourcepub fn limit(self, limit: usize) -> Self
pub fn limit(self, limit: usize) -> Self
Change max size of payload. By default max size is 32Kb
Sourcepub fn error_handler<F>(self, f: F) -> Self
pub fn error_handler<F>(self, f: F) -> Self
Set custom error handler
Trait Implementations§
Source§impl Clone for JsonConfig
impl Clone for JsonConfig
Source§fn clone(&self) -> JsonConfig
fn clone(&self) -> JsonConfig
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreAuto Trait Implementations§
impl Freeze for JsonConfig
impl !RefUnwindSafe for JsonConfig
impl Send for JsonConfig
impl Sync for JsonConfig
impl Unpin for JsonConfig
impl !UnwindSafe for JsonConfig
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more