Function actix_web_lab::middleware::map_response_body
source · pub fn map_response_body<F>(mapper_fn: F) -> MapResBodyMiddleware<F>
Expand description
Creates a middleware from an async function that is used as a mapping function for an
impl MessageBody
.
Examples
Completely replaces the body:
use actix_web::{body::MessageBody, HttpRequest};
async fn replace_body(
_req: HttpRequest,
_: impl MessageBody
) -> actix_web::Result<impl MessageBody> {
Ok("foo".to_owned())
}
Appends some bytes to the body:
use actix_web::{
body::{self, MessageBody},
web::{BufMut as _, BytesMut},
HttpRequest,
};
async fn append_bytes(
_req: HttpRequest,
body: impl MessageBody
) -> actix_web::Result<impl MessageBody> {
let buf = body::to_bytes(body).await.ok().unwrap();
let mut body = BytesMut::from(&buf[..]);
body.put_slice(b" - hope you like things ruining your payload format");
Ok(body)
}