request_body

Attribute Macro request_body 

Source
#[request_body]
Expand description

Extracts the raw request body into a specified variable.

This attribute macro extracts the raw request body content into a variable with the fixed type RequestBody. The body content is not parsed or deserialized.

§Usage

use hyperlane::*;
use hyperlane_macros::*;

#[route("/request_body")]
struct RequestBodyRoute;

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

    #[response_body(&format!("raw body: {raw_body:?}"))]
    #[request_body(raw_body)]
    async fn handle(self, ctx: &Context) {}
}

impl RequestBodyRoute {
    #[request_body(raw_body)]
    async fn request_body_with_ref_self(&self, ctx: &Context) {}
}

#[request_body(raw_body)]
async fn standalone_request_body_handler(ctx: &Context) {}

§Multi-Parameter Usage

use hyperlane::*;
use hyperlane_macros::*;

#[route("/multi_body")]
struct MultiBody;

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

    #[response_body(&format!("bodies: {body1:?}, {body2:?}"))]
    #[request_body(body1, body2)]
    async fn handle(self, ctx: &Context) {}
}

The macro accepts one or more variable names separated by commas. Each variable will be available in the function scope as a RequestBody type.