0

I am using actix-web 3. I would like to access the current status code from within Actix-web’s middleware system.

I would like to modify the request based on the StatusCode, is this possible?

For instance if the user sends some data that causes actix-web to generate the 413 code, I would like to modify the Request. I can modify the Response by the use of a handler of ErrorHandlers but these handlers only have mut res: ServiceResponse<B> as their arguments and it seems I am unable to find a way to access ServiceRequest object from within these handlers.

I would like to result to using basic middleware as the ServiceRequest and ServiceResponse objects are available in the middleware.

kmdreko
  • 42,554
  • 6
  • 57
  • 106
Allan K
  • 379
  • 2
  • 13

1 Answers1

1

The status code can be obtained by the use of the .status() method of the HttpResponse object from within the middleware code. The code that implements the Service trait has access to the HttpResponse as shown below.

Since the status is generated after sending the request, there is no way to know the status before hand so as to modify the request.

let fut = self.service.call(req);

Box::pin(async move {
    let res = fut.await?;
    println!("Hi from response, res.status is:'{}'", res.status());
    Ok(res)
})
kmdreko
  • 42,554
  • 6
  • 57
  • 106
Allan K
  • 379
  • 2
  • 13