Skip to content

Method guards

A method guard determines which HTTP methods are allowed for a given route. They are modelled by the MethodGuard type.

Single-method guards

The simplest case is a guard that allows a single HTTP method:

use pavex::blueprint::{router::GET /* (1)! */, Blueprint};
use pavex::f;

pub fn blueprint() -> Blueprint {
    let mut bp = Blueprint::new();
    bp.route(GET, "/greet", f!(crate::routes::greet));
    bp
}
  1. A short-hand for a MethodGuard that only allows GET requests.

This is by far the most common case and Pavex provides short-hands for it: in the pavex::blueprint::router module there is a pre-built guard for each well-known HTTP method (e.g. GET in the example above).

Multi-method guards

You can build a guard that accepts multiple HTTP methods by combining single-method guards with the or method:

use pavex::blueprint::router::{GET, HEAD};
use pavex::blueprint::Blueprint;
use pavex::f;

pub fn blueprint() -> Blueprint {
    let mut bp = Blueprint::new();
    bp.route(GET.or(HEAD), "/greet", f!(crate::routes::greet));
    bp
}

Ignoring the method

If you don't care about the HTTP method of the incoming request, use the ANY method guard:

use pavex::blueprint::{router::ANY, Blueprint};
use pavex::f;

pub fn blueprint() -> Blueprint {
    let mut bp = Blueprint::new();
    bp.route(ANY, "/greet", f!(crate::routes::greet));
    bp
}

ANY matches all well-known HTTP methods: GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS, CONNECT and TRACE.
It won't match, however, custom HTTP methods (e.g. FOO). If you truly want to match any HTTP method, use ANY_WITH_EXTENSIONS instead.