Available on crate feature normalize-path only.
Expand description

Middleware that normalizes paths.

Any trailing slashes from request paths will be removed. For example, a request with /foo/ will be changed to /foo before reaching the inner service.

Example

use tower_http::normalize_path::NormalizePathLayer;
use http::{Request, Response, StatusCode};
use hyper::Body;
use std::{iter::once, convert::Infallible};
use tower::{ServiceBuilder, Service, ServiceExt};

async fn handle(req: Request<Body>) -> Result<Response<Body>, Infallible> {
    // `req.uri().path()` will not have trailing slashes
}

let mut service = ServiceBuilder::new()
    // trim trailing slashes from paths
    .layer(NormalizePathLayer::trim_trailing_slash())
    .service_fn(handle);

// call the service
let request = Request::builder()
    // `handle` will see `/foo`
    .uri("/foo/")
    .body(Body::empty())?;

service.ready().await?.call(request).await?;

Structs

Middleware that normalizes paths.
Layer that applies NormalizePath which normalizes paths.