Potato
Home
Getting Started
GitHub
  • 简体中文
  • English
Home
Getting Started
GitHub
  • 简体中文
  • English
  • Preface
  • Getting Started
  • Handler Function Annotations
  • Handler Function Declaration
  • Server-Side Routing
  • Graceful Shutdown
  • Using the Client

Using the Client

Specify two parameters: the request address and additional parameters. Example code:

let res = potato::get("https://www.fawdlstty.com", vec![]).await?;
println!("{}", String::from_utf8(res.body)?);

Additional parameters are used to specify HTTP headers. Example of modifying User-Agent:

let res = potato::get("https://www.fawdlstty.com", vec![Headers::User_Agent("aaa".into())]).await?;
println!("{}", String::from_utf8(res.body)?);

Requests can be made in session form. If the request paths are the same, the connection will be reused:

let mut sess = Session::new();
let res1 = sess.get("https://www.fawdlstty.com/1", vec![]).await?;
let res2 = sess.get("https://www.fawdlstty.com/2", vec![]).await?;

To initiate a WebSocket connection request, use the following form:

let mut ws = Websocket::connect("ws://127.0.0.1:8080/ws", vec![]).await?;
ws.send_ping().await?;
ws.send_text("aaa").await?;
let frame = ws.recv().await?;

Additionally, even in pure client mode, you can use jemalloc to get detailed memory allocation reports. You need to add the following code at the program entry point (at the beginning of the main function):

potato::init_jemalloc()?;

Then when needed, call the following code:

let pdf_data = crate::dump_jemalloc_profile()?;

At this point, the pdf_data variable contains the raw content of the PDF memory analysis report. Store it as a file to view it.

Reverse Proxy and Transfer Sessions

You can use [TransferSession](file:///e:/GitHub_fa/potato/potato/src/client.rs#L224-L251) to handle reverse proxy and forward proxy scenarios. It supports forwarding of both HTTP and WebSocket requests, and can modify the forwarded content.

Create a reverse proxy session that forwards requests to a specified target URL:

let mut transfer_session = potato::client::TransferSession::from_reverse_proxy(
    "/api".to_string(),      // Request path prefix
    "http://backend-server:8080".to_string()  // Backend target server
);

// Use the transfer method when processing requests
// let response = transfer_session.transfer(&mut request, true /* whether to modify content */).await?;

Create a forward proxy session for general proxy forwarding:

let mut transfer_session = potato::client::TransferSession::from_forward_proxy();

// Use the transfer method when processing requests
// let response = transfer_session.transfer(&mut request, false /* whether to modify content */).await?;
Last Updated:: 1/3/26, 12:00 AM
Contributors: fawdlstty
Prev
Graceful Shutdown