Module tower_http::set_status

source ·
Available on crate feature set-status only.
Expand description

Middleware to override status codes.

Example

use tower_http::set_status::SetStatusLayer;
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> {
    // ...
}

let mut service = ServiceBuilder::new()
    // change the status to `404 Not Found` regardless what the inner service returns
    .layer(SetStatusLayer::new(StatusCode::NOT_FOUND))
    .service_fn(handle);

// Call the service.
let request = Request::builder().body(Body::empty())?;

let response = service.ready().await?.call(request).await?;

assert_eq!(response.status(), StatusCode::NOT_FOUND);

Structs