Attribute Macro get

#[get]
Expand description

Define a route for GET requests to a given path.

This is a shorthand for #[route(method = "GET", path = "...")].

§Example

use pavex::{get, Response};

#[get(path = "/users/{id}")]
pub async fn get_user(/* */) -> Response {
    // Get 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 GET route with your application in two ways:

The #[get] 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 get macro:

NameKindRequired
pathArgumentYes
idArgumentNo

§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::{get, Response};

#[get(path = "/users/{id}")]
//           👆 Path with parameter
pub async fn get_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::{get, Response, Blueprint};

#[get(path = "/users/{id}")]
pub async fn get_user(/* */) -> Response {
    // [...]
}

let mut bp = Blueprint::new();
// The generated constant is named `GET_USER`
bp.route(GET_USER);

Using a custom identifier:

use pavex::{get, Response, Blueprint};

#[get(path = "/users/{id}", id = "CUSTOM_ROUTE")]
//                           👆 Custom identifier
pub async fn get_user(id: u32) -> Response {
    // [...]
}

let mut bp = Blueprint::new();
// Use the custom identifier when registering
bp.route(CUSTOM_ROUTE);