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 http_body_util::Full;
use bytes::Bytes;
use std::{iter::once, convert::Infallible};
use tower::{ServiceBuilder, Service, ServiceExt};

async fn handle(req: Request<Full<Bytes>>) -> Result<Response<Full<Bytes>>, 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(Full::default())?;

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

Structs§