parse_json_body

Function parse_json_body 

Source
pub async fn parse_json_body<T>(req: Request<BoxBody>) -> Result<T, BoxError>
where T: for<'de> Deserialize<'de>,
Expand description

Parse a JSON body into the requested type T with header validation and a strict size limit.

The request must advertise an application/json (or +json) content type. Bodies larger than 1 MiB are rejected to guard against oversized payloads.

§Errors

  • Returns an error if the content type is missing or not JSON.
  • Returns an error when the body exceeds the configured size limit.
  • Returns an error if reading the body fails or the payload is invalid JSON.

§Examples

use bytes::Bytes;
use hyper::{Request, Response, StatusCode};
use hyperlite::{parse_json_body, BoxBody, BoxError};
use http_body_util::Full;
use serde::Deserialize;

#[derive(Deserialize)]
struct CreateGreeting { name: String }

async fn handler(req: Request<BoxBody>) -> Result<Response<Full<Bytes>>, BoxError> {
    let payload = parse_json_body::<CreateGreeting>(req).await?;
    Ok(hyperlite::success(StatusCode::CREATED, payload.name))
}