pub struct JsonConfig { /* private fields */ }Expand description
Json extractor configuration
Register it with WebAppConfig::set_value()
and App::with_config(). The extractor
does not read the application state, so a config stored in
AppState has no effect.
use ntex::http::error;
use ntex::web::{self, App, FromRequest, HttpResponse, WebAppConfig};
#[derive(serde::Deserialize)]
struct Info {
username: String,
}
/// deserialize `Info` from request's body, max payload size is 4kb
async fn index(info: web::types::Json<Info>) -> String {
format!("Welcome {}!", info.username)
}
fn main() {
let cfg = WebAppConfig::new()
.set_value(
// change json extractor configuration
web::types::JsonConfig::default()
.limit(4096)
.content_type(|mime| { // <- accept JSON and text/plain content types
mime.subtype() == mime::JSON
|| mime.suffix() == Some(mime::JSON)
|| (mime.type_() == mime::TEXT && mime.subtype() == mime::PLAIN)
}));
let app = App::default()
.with_config(cfg)
.service(web::resource("/index.html")
.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 content_type<F>(self, predicate: F) -> Self
pub fn content_type<F>(self, predicate: F) -> Self
Set predicate for allowed content types.
By default, JSON content types (*/json and */*+json) are accepted.
A predicate replaces this check: only content types for which it
returns true are accepted, so include the JSON types in the predicate
to keep accepting them. Requests without a valid Content-Type header
are always rejected.
Trait Implementations§
Source§impl Clone for JsonConfig
impl Clone for JsonConfig
Source§impl Debug for JsonConfig
impl Debug for JsonConfig
Auto Trait Implementations§
impl !RefUnwindSafe for JsonConfig
impl !UnwindSafe for JsonConfig
impl Freeze for JsonConfig
impl Send for JsonConfig
impl Sync for JsonConfig
impl Unpin for JsonConfig
impl UnsafeUnpin for JsonConfig
Blanket Implementations§
§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
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