response_header

Attribute Macro response_header 

Source
#[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 HEADER_NAME: &str = "X-Custom-Header";
const HEADER_VALUE: &str = "custom-value";

#[response_header("Content-Type", "application/json")]
async fn json_handler(ctx: Context) {
    // Response will have Content-Type header set to application/json
}

#[response_header("X-Static-Header" => "static-value")]
async fn set_header_handler(ctx: Context) {
    // Response will have static header replaced (overwrite existing)
}

#[response_header(HEADER_NAME, HEADER_VALUE)]
async fn dynamic_header_handler(ctx: Context) {
    // Response will have header from global constants
}

#[response_header("Cache-Control" => "no-cache")]
async fn set_cache_handler(ctx: Context) {
    // Response will have Cache-Control header replaced
}

#[response_header("X-Add-Header", "add-value")]
#[response_header("X-Set-Header" => "set-value")]
async fn header_operations_handler(ctx: Context) {
    // Response will have X-Add-Header set and X-Set-Header replaced
}

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 Context parameter.