Skip to main content

endpoint

Attribute Macro endpoint 

Source
#[endpoint]
Expand description

Transforms an HTTP handler function into a Dropshot endpoint.

The transformed function is suitable to be used as a parameter to ApiDescription::register().

The arguments to this macro encode information relevant to the operation of an API endpoint beyond what is expressed by the parameter and return types of a handler function.

§Arguments

The #[dropshot::endpoint] macro accepts the following arguments:

  • method: The HTTP request method (HTTP verb) for the endpoint. Can be one of DELETE, HEAD, GET, OPTIONS, PATCH, POST, or PUT. Required.
  • path: The path to the endpoint, along with path variables. Path variables are enclosed in curly braces. For example, path = "/widget/{id}". Required.
  • tags: An array of OpenAPI tags for the operation. Optional, defaults to an empty list.
  • versions: API versions for which the endpoint is valid. Optional. For more, see API Versioning.
  • content_type: The media type used to encode the request body. Can be one of application/json, application/x-www-form-urlencoded, or multipart/form-data. Optional, defaults to application/json.
  • deprecated: A boolean indicating whether the operation is marked deprecated in the OpenAPI document. Optional, defaults to false.
  • unpublished: A boolean indicating whether the operation is omitted from the OpenAPI document. Optional, defaults to false.
  • request_body_max_bytes: The maximum size of the request body in bytes. Accepts literals as well as constants of type usize. Optional, defaults to the server configuration’s default_request_body_max_bytes.

§Example: configuring an endpoint

const LARGE_REQUEST_BODY_MAX_BYTES: usize = 1 * 1024 * 1024;

#[endpoint {
    // --- Required fields ---
    // The HTTP method for the endpoint
    method = { DELETE | HEAD | GET | OPTIONS | PATCH | POST | PUT },
    // The path to the endpoint, along with path variables
    path = "/path/name/with/{named}/{variables}",

    // --- Optional fields ---
    // Tags for the operation's description
    tags = [ "all", "your", "OpenAPI", "tags" ],
    // API versions for which the endpoint is valid
    versions = "1.0.0".."2.0.0",
    // The media type used to encode the request body
    content_type = { "application/json" | "application/x-www-form-urlencoded" | "multipart/form-data" }
    // True if the operation is deprecated
    deprecated = { true | false },
    // True causes the operation to be omitted from the API description
    unpublished = { true | false },
    // Maximum request body size in bytes
    request_body_max_bytes = LARGE_REQUEST_BODY_MAX_BYTES,
}]
async fn my_endpoint(/* ... */) -> Result<HttpResponseOk, HttpError> {
    // ...
}