Skip to main content

response

Attribute Macro response 

Source
#[response]
Expand description

Helper attribute for method-level HTTP response customization.

This attribute is used within #[http] impl blocks to customize individual method responses. It is a no-op on its own.

§Supported Options

  • status = <code> - Custom HTTP status code (e.g., 201, 204)
  • content_type = "<type>" - Custom content type
  • header = "<name>", value = "<value>" - Add custom response header

Multiple #[response(...)] attributes can be combined on a single method.

§Examples

#[http(prefix = "/api")]
impl MyService {
    // Custom status code for creation
    #[response(status = 201)]
    fn create_item(&self, name: String) -> Item { /* ... */ }

    // No content response
    #[response(status = 204)]
    fn delete_item(&self, id: String) { /* ... */ }

    // Binary response with custom content type
    #[response(content_type = "application/octet-stream")]
    fn download(&self, id: String) -> Vec<u8> { /* ... */ }

    // Add custom headers
    #[response(header = "X-Custom", value = "foo")]
    fn with_header(&self) -> String { /* ... */ }

    // Combine multiple response attributes
    #[response(status = 201)]
    #[response(header = "Location", value = "/api/items/123")]
    #[response(header = "X-Request-Id", value = "abc")]
    fn create_with_headers(&self, name: String) -> Item { /* ... */ }
}