#[response_header]Expand description
Sets or replaces a specific HTTP response header.
This attribute macro configures a specific HTTP response header that will be sent with the response.
Both the header name and value can be provided as string literals or global constants.
Use "key", "value" to set a header (add to existing headers) or "key" => "value" to replace a header (overwrite existing).
ยงUsage
use hyperlane::*;
use hyperlane_macros::*;
const CUSTOM_HEADER_NAME: &str = "X-Custom-Header";
const CUSTOM_HEADER_VALUE: &str = "custom-value";
#[route("/response_header")]
struct ResponseHeader;
impl ServerHook for ResponseHeader {
async fn new(_ctx: &mut Context) -> Self {
Self
}
#[response_header(CUSTOM_HEADER_NAME => CUSTOM_HEADER_VALUE)]
async fn handle(self, ctx: &mut Context) {}
}
impl ResponseHeader {
#[response_header(CUSTOM_HEADER_NAME => CUSTOM_HEADER_VALUE)]
async fn response_header_with_ref_self(&self, ctx: &mut Context) {}
}
#[route("/response_header")]
struct ResponseHeaderTest;
impl ServerHook for ResponseHeaderTest {
async fn new(_ctx: &mut Context) -> Self {
Self
}
#[response_body("Testing header set and replace operations")]
#[response_header("X-Add-Header", "add-value")]
#[response_header("X-Set-Header" => "set-value")]
async fn handle(self, ctx: &mut Context) {}
}
#[response_header("X-Custom" => "value")]
async fn standalone_response_header_handler(ctx: &mut Context) {}The macro accepts header name and header value, both can be string literals or global constants.
Use "key", "value" for setting headers and "key" => "value" for replacing headers.
Should be applied to async functions that accept a &mut Context parameter.