Skip to content

处理body信息

同步打印body信息

Rust
fn intercept_response(mut response: Response<Incoming>) -> Response<HttpBody> {
     // 打印响应信息
     //Collect the body frames
    let mut body_data = Vec::new();
    let body = response.body_mut();
    while let Some(frame) = futures::executor::block_on(body.frame()) {
        match frame {
            Ok(frame) => {
                if let Some(bytes) = frame.data_ref() {
                    body_data.extend_from_slice(bytes);
                    println!("{:?}", "test");
                }
            }
            Err(e) => {
                println!("Error reading frame: {:?}", e);
                break;
            }
        }
    }

    // Convert the collected body data to a string
    let body_string = String::from_utf8_lossy(&body_data);
    dbg!({ format!("{:?}", body_string) });
     response
            .headers_mut()
            .insert("proxy-server", "Proxylea".parse().unwrap());
        //let (parts,incoming)=resp.into_parts();
        let resp = response.map(|b| {
            b.map_frame(|frame| {
                if let Some(bytes) = frame.data_ref() {
                    //
                }
                frame
            })
            .boxed()
        });
        resp
  }

异步打印body 信息

  1. 获取body内容得到string
  2. 只打印content-type: 'application/json'的信息
Rust
/// Intercept remote responses
async fn intercept_response(mut response: Response<Incoming>) -> Response<HttpBody> {
    dbg!({ format!("{:?}", response.headers()) });

    // new_response
    let content_type = response
        .headers()
        .get(header::CONTENT_TYPE)
        .map(|v| v.to_str().unwrap_or(""));

    if content_type == Some("application/json") {
        println!("content-type: json");
        // Collect the body frames
        let body_data = response.body_mut().collect().await.unwrap().to_bytes();

        // Convert the collected body data to a string
        let body_string = String::from_utf8_lossy(&body_data);
        dbg!({ format!("{:?}", body_string) });
        // Reconstruct the response with the collected body
        let mut new_response = Response::builder()
            .status(response.status())
            .body(full(body_data))
            .unwrap();

        // Copy headers from the original response to the new response
        *new_response.headers_mut() = response.headers().clone();
        new_response
            .headers_mut()
            .insert("proxy-server", "Proxylea".parse().unwrap());

        new_response
    } else {
        // If not JSON, just pass the response through without modification
        // response.headers_mut().insert("proxy-server", "Proxylea".parse().unwrap());
        // response.map(|b| b.boxed())
        response
            .headers_mut()
            .insert("proxy-server", "Proxylea".parse().unwrap());
        //let (parts,incoming)=resp.into_parts();
        let resp = response.map(|b| {
            b.map_frame(|frame| {
                if let Some(bytes) = frame.data_ref() {
                    //
                }
                frame
            })
            .boxed()
        });
        resp
    }
}

前端知识体系 · wcrane