Struct rocket::serde::msgpack::MsgPack [−][src]
pub struct MsgPack<T>(pub T);
msgpack
only.Expand description
The MessagePack guard: easily consume and return MessagePack.
Receiving MessagePack
MsgPack
is both a data guard and a form guard.
Data Guard
To parse request body data as MessagePack , add a data
route argument with
a target type of MsgPack<T>
, where T
is some type you’d like to parse
from JSON. T
must implement serde::Deserialize
.
use rocket::serde::msgpack::MsgPack; #[post("/users", format = "msgpack", data = "<user>")] fn new_user(user: MsgPack<User>) { /* ... */ }
You don’t need to use format = "msgpack"
, but it may be what you want.
Using format = msgpack
means that any request that doesn’t specify
“application/msgpack” as its first Content-Type:
header parameter will not
be routed to this handler.
Form Guard
MsgPack<T>
, as a form guard, accepts value and data fields and parses the
data as a T
. Simple use MsgPack<T>
:
use rocket::form::{Form, FromForm}; use rocket::serde::msgpack::MsgPack; #[derive(FromForm)] struct User<'r> { name: &'r str, metadata: MsgPack<Metadata> } #[post("/users", data = "<form>")] fn new_user(form: Form<User<'_>>) { /* ... */ }
Incoming Data Limits
The default size limit for incoming MessagePack data is 1MiB. Setting a
limit protects your application from denial of service (DOS) attacks and
from resource exhaustion through high memory consumption. The limit can be
increased by setting the limits.msgpack
configuration parameter. For
instance, to increase the MessagePack limit to 5MiB for all environments,
you may add the following to your Rocket.toml
:
[global.limits]
msgpack = 5242880
Sending MessagePack
If you’re responding with MessagePack data, return a MsgPack<T>
type,
where T
implements Serialize
from serde
. The content type of the
response is set to application/msgpack
automatically.
use rocket::serde::msgpack::MsgPack; #[get("/users/<id>")] fn user(id: usize) -> MsgPack<User> { let user_from_id = User::from(id); /* ... */ MsgPack(user_from_id) }
Implementations
Consumes the MsgPack
wrapper and returns the wrapped item.
Example
let string = "Hello".to_string(); let my_msgpack = MsgPack(string); assert_eq!(my_msgpack.into_inner(), "Hello".to_string());
Trait Implementations
Asynchronously validates, parses, and converts an instance of Self
from the incoming request body data. Read more
Serializes the wrapped value into MessagePack. Returns a response with
Content-Type MsgPack
and a fixed-size body with the serialization. If
serialization fails, an Err
of Status::InternalServerError
is returned.
Returns Ok
if a Response
could be generated successfully. Otherwise,
returns an Err
with a failing Status
. Read more
Auto Trait Implementations
impl<T> RefUnwindSafe for MsgPack<T> where
T: RefUnwindSafe,
impl<T> UnwindSafe for MsgPack<T> where
T: UnwindSafe,