Skip to main content

param

Attribute Macro param 

Source
#[param]
Expand description

Helper attribute for parameter-level HTTP customization.

This attribute is used on function parameters within #[http] impl blocks to customize parameter extraction and naming. It is a no-op on its own.

Note: Requires nightly Rust with #![feature(register_tool)] and #![register_tool(param)] at the crate root.

§Supported Options

  • name = "<wire_name>" - Use a different name on the wire (e.g., q instead of query)
  • default = <value> - Provide a default value for optional parameters
  • query - Force parameter to come from query string
  • path - Force parameter to come from URL path
  • body - Force parameter to come from request body
  • header - Extract parameter from HTTP header

§Location Inference

When no location is specified, parameters are inferred based on conventions:

  • Parameters named id or ending in _id → path parameters
  • POST/PUT/PATCH methods → body parameters
  • GET/DELETE methods → query parameters

§Examples

#![feature(register_tool)]
#![register_tool(param)]

#[http(prefix = "/api")]
impl SearchService {
    // Rename parameter: code uses `query`, API accepts `q`
    fn search(&self, #[param(name = "q")] query: String) -> Vec<Result> {
        /* ... */
    }

    // Default value for pagination
    fn list_items(
        &self,
        #[param(default = 0)] offset: u32,
        #[param(default = 10)] limit: u32,
    ) -> Vec<Item> {
        /* ... */
    }

    // Extract API key from header
    fn protected_endpoint(
        &self,
        #[param(header, name = "X-API-Key")] api_key: String,
        data: String,
    ) -> String {
        /* ... */
    }

    // Override location inference: force to query even though method is POST
    fn search_posts(
        &self,
        #[param(query)] filter: String,
        #[param(body)] content: String,
    ) -> Vec<Post> {
        /* ... */
    }

    // Combine multiple options
    fn advanced(
        &self,
        #[param(query, name = "page", default = 1)] page_num: u32,
    ) -> Vec<Item> {
        /* ... */
    }
}

§OpenAPI Integration

  • Parameters with name are documented with their wire names
  • Parameters with default are marked as not required
  • Location overrides are reflected in OpenAPI specs