Attribute Macro hx_post

Source
#[hx_post]
Expand description

A macro that generates HTMX-compatible statically-typed Post routes for axum handlers.

§Syntax

#[hx_post("<PATH>" [with <STATE>])]
  • PATH is the path of the route, with optional path parameters and query parameters, e.g. /item/:id?amount&offset.
  • STATE is the type of axum-state, passed to the handler. This is optional, and if not specified, the state type is guessed based on the parameters of the handler.

§Example

use axum::extract::{State, Json};
use axum_routing_htmx::hx_post;

#[hx_post("/item/:id?amount&offset")]
async fn item_handler(
    id: u32,
    amount: Option<u32>,
    offset: Option<u32>,
    State(state): State<String>,
    Json(json): Json<u32>,
) -> String {
    todo!("handle request")
}

§State type

Normally, the state-type is guessed based on the parameters of the function: If the function has a parameter of type [..]::State<T>, then T is used as the state type. This should work for most cases, however when not sufficient, the state type can be specified explicitly using the with keyword:

#[hx_post("/item/:id?amount&offset" with String)]

§Internals

The macro expands to a function that returns an [HtmxHandler<S>].