Skip to content

Middleware example for 4.0.0 #2597

Answered by aliemjay
GreenTeaSeb asked this question in Q&A
Discussion options

You must be logged in to vote

You'll need EitherBody and ServiceResponse::{map_into_right_body, map_into_left_body}

type Response = ServiceResponse<EitherBody<B, BoxBody>>;

fn call() {
  let service = Rc::clone(&self.service);
  Box::pin(async move {
    if true {
      Ok(service.call(req)?.await.map_into_left_body())
    } else {
      let resp = HttpResponse::Ok().finish();
      Ok(req.into_response(resp).map_into_right_body())
    }
  })
}

alternatively you can box the response body:

type Response = ServiceResponse<BoxBody>; // or `ServiceResponse`

fn call() {
  let service = Rc::clone(&self.service);
  Box::pin(async move {
    if true {
      Ok(service.call(req)?.await.map_into_boxed_body())
    } else {
      

Replies: 4 comments 3 replies

Comment options

You must be logged in to vote
2 replies
@GreenTeaSeb
Comment options

@lizhongit
Comment options

Answer selected by GreenTeaSeb
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@robjtede
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
7 participants