Attribute Macro post
#[post]Expand description
Define a route for POST requests to a given path.
This is a shorthand for #[route(method = "POST", path = "...")].
§Example
use pavex::{post, Response};
#[post(path = "/users")]
pub async fn create_user(/* */) -> Response {
// Create user logic
// [...]
}§Guide
Check out the “Routing” section of Pavex’s guide for a thorough introduction to routing in Pavex applications.
§Registration
You can register a POST route with your application in two ways:
- Use
Blueprint::routeto register a single route - Use
Blueprint::importto import multiple routes in bulk
The #[post] macro generates a constant that you can use to refer to the
route when invoking Blueprint::route.
§Arguments
The sections below provide an exhaustive list of all the arguments supported by the post macro:
§path
The path argument specifies the URL path pattern that this route will match.
The path can contain dynamic parameter placeholders in the format {parameter_name}.
Check out the guide on parameter extraction
for a detailed explanation.
§Example
use pavex::{post, Response};
#[post(path = "/users")]
// 👆 Path with parameter
pub async fn create_user(/* */) -> Response {
// [...]
}§id
By default, Pavex generates a constant named after your function
(converted to UPPER_SNAKE_CASE) that you can use when invoking Blueprint::route.
The id argument allows you to customize the name of the generated constant.
§Example
Using the default generated identifier:
use pavex::{post, Response, Blueprint};
#[post(path = "/users")]
pub async fn create_user(/* */) -> Response {
// [...]
}
let mut bp = Blueprint::new();
// The generated constant is named `CREATE_USER`
bp.route(CREATE_USER);Using a custom identifier:
use pavex::{post, Response, Blueprint};
#[post(path = "/users", id = "CUSTOM_ROUTE")]
// 👆 Custom identifier
pub async fn create_user(id: u32) -> Response {
// [...]
}
let mut bp = Blueprint::new();
// Use the custom identifier when registering
bp.route(CUSTOM_ROUTE);