Potato
Home
入门示例
GitHub
  • 简体中文
  • English
Home
入门示例
GitHub
  • 简体中文
  • English
  • 前言
  • 入门示例
  • 处理函数标注与声明
  • 服务端路由
  • 优雅退出
  • 使用客户端
  • SSE(流式传输)
  • ACME自动证书管理
  • WebTransport 支持
  • 缓存系统
  • Controller

使用客户端

客户端宏支持直接传 URL,并可选追加请求头:

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

// 带请求头
let mut res = potato::get!("https://www.fawdlstty.com", User_Agent = "my-client").await?;

// POST/PUT 第二个参数是 body
let body = vec![];
let mut res = potato::post!("https://www.fawdlstty.com", body, Content_Type = "application/json").await?;

所有 HTTP 方法宏(get!、post!、put!、delete!、patch!、head!、options!、trace!、connect!)均支持此语法。

HTTP/2 和 HTTP/3 请求

使用 http2() 或 http3() 包装器指定协议版本(默认 HTTP/1.1):

#[cfg(feature = "http2")]
let mut res = potato::get!(http2("https://www.fawdlstty.com")).await?;

#[cfg(feature = "http3")]
let mut res = potato::post!(http3("https://api.example.com"), body, Custom("X-Key") = "value").await?;
  • HTTP/1.1: 默认,直接使用 URL
  • HTTP/2: 需 TLS,使用 http2("https://...")
  • HTTP/3: 需 TLS(基于 QUIC),使用 http3("https://...")

完整示例:examples/05_http2_http3_client.rs

会话与流式

会话复用(相同路径复用连接):

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?;

SSE 流式响应:

let mut res = potato::get!("http://127.0.0.1:3000/api/v1/chat").await?;
let mut stream = res.body.stream_data();
while let Some(chunk) = stream.next().await {
    print!("{}", String::from_utf8_lossy(&chunk));
}

WebSocket 连接:

let mut ws = potato::websocket!("ws://127.0.0.1:8080/ws", Custom("X-Key") = "value").await?;
ws.send_text("hello").await?;
let frame = ws.recv().await?;

自定义 HTTP Header

所有客户端宏支持混合使用标准 Header 和 Custom Header:

let res = potato::get!(
    "https://api.example.com/data",
    Custom("X-Custom-Header") = "value",  // Custom header(字符串 key)
    User_Agent = "my-client/1.0",          // Standard header(标识符)
    Custom(k) = v                          // 支持变量
).await?;

语法规则:

  • 标准 Header: 标识符(如 User_Agent、Content_Type)
  • Custom Header: Custom(key) = value,key/value 可为字符串或变量
  • 混合使用,逗号分隔,支持尾随逗号

完整示例:examples/01_client_with_arg.rs

其他功能

Jemalloc 内存分析(需启用 jemalloc feature):

potato::init_jemalloc()?;  // main 函数开始处
// ... 运行程序 ...
let pdf_data = potato::dump_jemalloc_profile()?;  // 获取 PDF 报告

反向代理与转发会话:

// 反向代理
let mut session = potato::client::TransferSession::from_reverse_proxy(
    "/api", "http://backend-server:8080"
);

// 正向代理
let mut session = potato::client::TransferSession::from_forward_proxy();

// 使用: session.transfer(&mut request, modify_content).await?

Agent 客户端会话

多轮对话 LLM 客户端,支持 OpenAI、Anthropic、Ollama、OpenCode、Codex 等提供商:

// 创建会话
let mut agent = potato::AgentClientSession::new(
    potato::LlmProvider::OpenAI,
    "https://api.openai.com",
    Some("sk-your-api-key".to_string()),
);

// 设置系统提示词与模型
agent.set_system_prompt("You are a helpful assistant.");
agent.set_model("gpt-4o-mini").await?;

// 非流式对话
let reply = agent.chat("Hello!").await?;

// 流式对话
let mut stream = agent.chat_stream("Tell me a story.").await?;
while let Some(chunk) = stream.recv().await {
    match chunk {
        potato::StreamChunk::Content(text) => print!("{}", text),
        potato::StreamChunk::Done => break,
    }
}

// 获取模型列表
let models = agent.list_models().await?;

// 序列化与恢复会话状态
let state = agent.serialize()?;
let mut restored = potato::AgentClientSession::deserialize(&state)?;

完整示例:examples/client/09_agent_session.rs

最近更新:: 2026/4/15 23:00
Contributors: fawdlstty
Prev
优雅退出
Next
SSE(流式传输)