tower_http

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

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

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

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

Structs§