Tower Http RequestDecompressionLayer Example request #2031
Unanswered
theelderbeever
asked this question in
Q&A
Replies: 2 comments 9 replies
-
I don't really understand what you're trying to do. When you call If you just wanna decompress all requests you need to use use axum::{
body::Body,
error_handling::HandleErrorLayer,
http::{Request, Response, StatusCode},
routing::post,
BoxError, Json, Router,
};
use bytes::BytesMut;
use http_body::Body as _;
use serde_json::Value;
use std::str::FromStr;
use tower::{service_fn, ServiceBuilder};
use tower_http::decompression::{DecompressionBody, RequestDecompressionLayer};
#[tokio::main(flavor = "current_thread")]
async fn main() {
let app: Router = Router::new().route("/", post(root)).layer(
ServiceBuilder::new()
.layer(HandleErrorLayer::new(|error: BoxError| async move {
(StatusCode::INTERNAL_SERVER_ERROR, "Unhandled server error")
}))
.layer(RequestDecompressionLayer::new()),
);
let addr = std::net::SocketAddr::from_str(&format!("{}:{}", "127.0.0.1", 8000)).unwrap();
axum::Server::bind(&addr)
.serve(app.into_make_service())
.await
.unwrap();
}
async fn root(Json(value): Json<Value>) -> Json<Value> {
Json(value)
} |
Beta Was this translation helpful? Give feedback.
8 replies
-
I get:
How do you get |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hopefully I am just missing something obvious however, I am trying to build a thin api that receives metrics data. To this end the api needs to be able to handle compressed request bodies that are hinted by the
Content-Encoding: deflate
(or others) header. I have been looking at the RequestDecompressionLayer and trying to implement it. However, I keep getting a trait impl error that I can't make sense of and have no clue how to fix. Below is some example code to make the error show up. Its taken almost exactly from the tower_http example. If you remove the layer it is just a post echo server right now. However, I would like to be able to send a compressed payload and then read it as json. Any help would be greatly appreciated.The Error
Beta Was this translation helpful? Give feedback.
All reactions