JSON
You can use JsonBody<T>
to work with JSON-encoded request bodies.
JsonBody<T>
parses the raw JSON into an instance of the type T
you specified.
use pavex::http::StatusCode;
use pavex::post;
use pavex::request::body::JsonBody;
#[derive(serde::Deserialize)]
pub struct HomeListing {
address: String,
price: u64,
}
#[post(path = "/listing")]
pub fn create_listing(body: &JsonBody<HomeListing> ) -> StatusCode {
println!(
"The home you want to sell for ${} is located in {}",
body.0.price, body.0.address
);
// [...]
}
The whole request body is buffered in memory before being deserialized.
Imports
To use JsonBody
in your application, you need to import its constructor from pavex
:
use pavex::{Blueprint, blueprint::from};
pub fn blueprint() -> Blueprint {
let mut bp = Blueprint::new();
bp.import(from![pavex]);
// [...]
}
Deserialization
The newly defined struct must be deserializable—i.e. it must implement the serde::Deserialize
trait.
You can derive serde::Deserialize
in most cases.
Avoiding allocations
If you want to minimize memory usage, you can try to avoid unnecessary heap memory allocations when deserializing string-like fields from the body of the incoming request. Pavex supports this use case—you can borrow from the request body.
Escape sequences
It is not always possible to avoid allocations, though.
In particular,
Pavex must allocate a new String
if the JSON string you are trying to deserialize contains escape sequences,
such as \n
or a \"
.
Using a &str
in this case would result in a runtime error when attempting the deserialization.
Cow
We recommend using Cow<'_, str>
as your field type for string-like parameters.
It borrows from the request's body if possible, it allocates a new String
if it can't be avoided.
Cow<'_, str>
strikes a balance between performance and robustness: you don't have to worry about a runtime error
if the field contains escape sequences, but you tried to use &str
as its field type.