You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Reqwest调用Meilisearch生成API时报“builder error”且无请求发出

问题

使用Reqwest crate向Meilisearch发送HTTP请求生成API密钥时,返回无意义的“builder error”错误,且无请求到达Meilisearch的7700端口。相关代码、错误响应及服务端日志如下:

相关代码

async fn generate_meili_api_key(state: &AppState, username: &str) -> Result<String, String> {
    println!("Preparing payload");

    let api_key_payload = json!({
        "name": format!("arcus-{}", username),
        "description": format!("Temporary key for user {}", username),
        "indexes": [format!("arcus-{}", username)],         // Modify this to limit to user-specific indices if required
        "actions": ["search", "documents.add", "tasks.get"],     // Limit to search actions
        "expiresAt": (Utc::now() + Duration::hours(12)).format("%Y-%m-%dT%H:%M:%SZ").to_string(), // Set an expiration date for the key
    });

    println!("Sending {api_key_payload} to {} with header {}",format!("{}/keys", state.meili_url), format!("Bearer {}", state.master_key));

    let response = state
        .client
        .post(format!("{}/keys", state.meili_url))
        .header("Authorization", format!("Bearer {}", state.master_key))
        .json(&api_key_payload)
        .send()
        .await
        .map_err(|e| format!("Failed to obtain key from meilisearch: {e}"))? // e -> "builder error"
        .json::<serde_json::Value>()
        .await.map_err(|_| "Failed to parse meilisearch response")?;

    println!("Response: {}", response);

    if let Some(key) = response["key"].as_str() {
        Ok(key.to_string())
    } else {
        Err("Meilisearch response did not contain an API key".to_owned())
    }
}

客户端错误响应

❯ curl -X POST http://127.0.0.1:3000/meilikeys --header "Content-Type: application/json" --data '{ "username": "user", "password": "password" }'
{"message":"Failed to generate API key: Failed to obtain key from meilisearch: builder error","api_key":null}⏎

服务端日志

Preparing payload
Sending {"actions":["search","documents.add","tasks.get"],"description":"Temporary key for user user","expiresAt":"2024-10-29T11:52:18Z","indexes":["arcus-user"],"name":"arcus-user"} to http://127.0.0.1:7700/keys with header Bearer <correct-token-here>

排查方向与解决方案

  • 检查Reqwest客户端初始化:"builder error"属于请求构建阶段的错误,而非发送阶段。确认AppState中的client是否正确初始化,比如是否配置了无效代理、错误TLS参数,或是未添加tls等必要特性(如reqwest依赖未启用tls特性会导致HTTPS请求构建失败)。
  • 修复请求URL格式:检查state.meili_url末尾是否带斜杠,若meili_url为http://127.0.0.1:7700/,拼接后会生成http://127.0.0.1:7700//keys这种无效URL,触发构建错误。可改用format!("{}/keys", state.meili_url.trim_end_matches('/'))修复。
  • 验证Authorization头合法性:确认state.master_key是否为空或包含非法字符(如换行、空格),空值或非法字符会导致Authorization头格式无效,引发构建失败。
  • 排查JSON序列化问题:虽然日志显示payload格式正确,但json!宏可能存在隐藏的序列化异常。尝试手动构建serde_json::Value结构体,或定义自定义序列化结构体替代json!,确保序列化过程无错误。
  • 开启Reqwest详细日志:添加tracing或log依赖,启用Reqwest的debug日志(如在Cargo.toml中设置reqwest = { version = "0.11", features = ["json", "logging"] }),查看请求构建阶段的具体错误细节,定位故障点。

内容的提问来源于stack exchange,提问作者NoBullsh1t

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 16:40:58