pub struct JsonBody<T>(pub T);
Expand description
Parse the body of an incoming request as JSON.
§Guide
Check out the relevant section
of Pavex’s guide for a thorough introduction to JsonBody
.
§Example
use pavex::request::body::JsonBody;
// You must derive `serde::Deserialize` for the type you want to extract,
// in this case `HomeListing`.
#[derive(serde::Deserialize)]
pub struct HomeListing {
address: String,
price: u64,
}
// The `Json` extractor deserializes the request body into
// the type you specified—`HomeListing` in this case.
pub fn get_home(body: &JsonBody<HomeListing>) -> String {
format!(
"The home you want to sell for {} is located at {}",
body.0.price,
body.0.address
)
}
Tuple Fields§
§0: T
Implementations§
source§impl<T> JsonBody<T>
impl<T> JsonBody<T>
sourcepub fn extract<'head, 'body>(
request_head: &'head RequestHead,
buffered_body: &'body BufferedBody,
) -> Result<Self, ExtractJsonBodyError>where
T: Deserialize<'body>,
pub fn extract<'head, 'body>(
request_head: &'head RequestHead,
buffered_body: &'body BufferedBody,
) -> Result<Self, ExtractJsonBodyError>where
T: Deserialize<'body>,
The default constructor for JsonBody
.
The extraction can fail for a number of reasons:
- the
Content-Type
is missing - the
Content-Type
header is not set toapplication/json
or anotherapplication/*+json
MIME type - the request body is not a valid JSON document
In all of the above cases, an ExtractJsonBodyError
is returned.
source§impl JsonBody<()>
impl JsonBody<()>
sourcepub fn register(bp: &mut Blueprint) -> RegisteredConstructor<'_>
pub fn register(bp: &mut Blueprint) -> RegisteredConstructor<'_>
Register the default constructor
for JsonBody
with a Blueprint
.
sourcepub fn default_constructor() -> Constructor
pub fn default_constructor() -> Constructor
The default constructor
and error handler for JsonBody
.
Trait Implementations§
Auto Trait Implementations§
impl<T> Freeze for JsonBody<T>where
T: Freeze,
impl<T> RefUnwindSafe for JsonBody<T>where
T: RefUnwindSafe,
impl<T> Send for JsonBody<T>where
T: Send,
impl<T> Sync for JsonBody<T>where
T: Sync,
impl<T> Unpin for JsonBody<T>where
T: Unpin,
impl<T> UnwindSafe for JsonBody<T>where
T: UnwindSafe,
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