request_body_json

Attribute Macro request_body_json 

Source
#[request_body_json]
Expand description

Parses the request body as JSON into a specified variable and type.

This attribute macro extracts and deserializes the request body content as JSON into a variable with the specified type. The body content is parsed as JSON using serde.

ยงUsage

use hyperlane::*;
use hyperlane_macros::*;
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize, Clone)]
struct TestData {
    name: String,
    age: u32,
}

#[route("/request_body_json")]
struct RequestBodyJson;

impl ServerHook for RequestBodyJson {
    async fn new(_ctx: &Context) -> Self {
        Self
    }

    #[response_body(&format!("request data: {request_data_result:?}"))]
    #[request_body_json(request_data_result: TestData)]
    async fn handle(self, ctx: &Context) {}
}

The macro accepts a variable name and type in the format variable_name: Type. The variable will be available in the function scope as a Result<Type, JsonError>.