Skip to content

How to fetch response body from tower::util::MapResponseLayer ? #208

Answered by davidpdrsn
xiuno asked this question in Q&A
Discussion options

You must be logged in to vote

You should be able to use AndThenLayer from tower. Its basically an async map_response that can return a result as well.

Something like this:

use axum::{
    body::{box_body, Body, BoxBody},
    handler::get,
    http::{Response, StatusCode},
    route,
    routing::RoutingDsl,
};
use std::net::SocketAddr;

#[tokio::main]
async fn main() {
    let app = route("/", get(|| async {}))
        // make sure this is added as the very last thing
        .layer(tower::util::AndThenLayer::new(map_404));

    // run it
    let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
    axum::Server::bind(&addr)
        .serve(app.into_make_service())
        .await
        .unwrap();
}

async fn map_404(

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by davidpdrsn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #207 on August 19, 2021 09:49.