[][src]Function hyperbole::prelude::body::json

pub async fn json<T: DeserializeOwned>(
    cx: HCons<Body, HNil>
) -> Result<HCons<T, HNil>, JsonBodyError>

Deserialize a value from a json request body.

Use with Ctx::handle_with or Ctx::try_then.

Examples

use hyperbole::{body::json, record_args, uri, Ctx};
use serde::Deserialize;

#[derive(Deserialize)]
struct ThingRequest {
    x: u32,
    y: String,
}

#[record_args]
async fn the_thing(_: ThingRequest) -> &'static str {
    "yepperz"
}

let _ctx = Ctx::default()
    // inline with get_with:
    .get_with(uri!["the-thing"], json::<ThingRequest>, the_thing)
    // or as a middleware:
    .try_then(json::<ThingRequest>)
    .get(uri!["the-thing" / "via-mw"], the_thing);