Expand description
Django-shape @require_http_methods(['GET', 'POST']) / @require_GET
/ @require_POST / @require_safe decorators as tower
middleware. Rejects unallowed methods with 405 Method Not Allowed + RFC 7231-compliant Allow: header listing the
accepted set.
Gated on _axum — it is tower middleware over axum::extract::Request,
and axum is an optional dependency absent from a bare-ORM build.
Django-shape HTTP method restriction layer — mirrors
django.views.decorators.http.{require_http_methods, require_GET, require_POST, require_safe}.
Django uses these as decorators on view functions:
@require_http_methods(["GET", "POST"])
def my_view(request):
...
@require_POST
def submit(request):
...axum routes are method-aware out of the box (Router::route("/x", get(handler)) only ever sees GETs), so the decorator pattern
looks redundant — until you have a single handler that should
accept e.g. both GET and POST but not PUT/DELETE/PATCH. Mounting
MethodRestrictLayer::any_of(["GET", "POST"]) lets a single
Router::route("/x", any(handler)) enforce that without
re-routing.
Returns 405 Method Not Allowed with an RFC 7231-compliant
Allow: header listing the accepted methods (browsers and HTTP
caches use the header for negotiation).
§Usage
use axum::{Router, routing::any};
use rustango::http_methods::{MethodRestrictLayer, MethodRestrictRouterExt};
let app: Router = Router::new()
.route("/submit", any(handler))
.require_methods(["POST"]); // POST only
let mixed: Router = Router::new()
.route("/feed", any(handler))
.require_methods(["GET", "HEAD", "POST"]); // multi-method view
// Convenience shortcuts mirror Django's per-verb decorators.
let safe_only: Router = Router::new()
.route("/health", any(handler))
.require_safe(); // GET / HEAD / OPTIONSStructs§
- Method
Restrict Layer - Tower layer that gates a route on a fixed set of HTTP methods.
Rejects unmatched methods with
405 Method Not Allowed+ canonicalAllow:header. Constructed viaSelf::any_ofor theMethodRestrictRouterExtconvenience methods.
Traits§
- Method
Restrict Router Ext - Extension trait —
router.require_methods([…])reads better at the call site than.layer(MethodRestrictLayer::any_of([…])).