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

Deribit测试API POST请求失败问题排查求助

问题原因及修正方案

核心错误:参数不符合Deribit API规范

你遇到的bad_request(code:11050)是典型的参数格式错误,具体问题如下:

  1. 市价单参数设置错误
    Deribit的private/buy接口中,市价单不需要设置price字段,而是要通过type字段指定订单类型为market。你当前把price设为"market"完全不符合接口要求,这是触发错误的主要原因。

  2. 下单数量类型错误
    Deribit合约的下单数量要求是整数(比如永续合约的单位是"张"),你传入的double类型数值会被API判定为非法参数。

  3. 内存泄漏隐患
    代码中没有调用curl_slist_free_all(headers)清理HTTP头链表,会导致内存泄漏,需要补充该操作。

修正后的代码片段

调整payload参数并修复内存泄漏问题:

bool placeOrder(const std::string& access_token, const std::string& instrument_name, double amount) {
    CURL* curl = curl_easy_init();
    if (!curl) {
        std::cerr << "Failed to initialize curl" << std::endl;
        return false;
    }

    curl_easy_setopt(curl, CURLOPT_CAINFO, "C:/Program Files/curl/cacert.pem");

    std::string order_url = "https://test.deribit.com/api/v2/private/buy";
    // 修正:移除price,添加type字段,将amount转为整数
    json payload = {
        {"instrument_name", instrument_name},
        {"amount", static_cast<int>(amount)},
        {"type", "market"},
        {"label", "market0000234"}
    };
    std::string post_fields = payload.dump();
    std::string response;

    struct curl_slist* headers = nullptr;
    headers = curl_slist_append(headers, ("Authorization: Bearer " + access_token).c_str());
    headers = curl_slist_append(headers, "Content-Type: application/json");

    curl_easy_setopt(curl, CURLOPT_URL, order_url.c_str());
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_fields.c_str());
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);

    CURLcode res = curl_easy_perform(curl);
    if (res != CURLE_OK) {
        std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl;
        curl_slist_free_all(headers); // 提前清理
        curl_easy_cleanup(curl);
        return false;
    }

    curl_slist_free_all(headers); // 清理HTTP头链表
    curl_easy_cleanup(curl);

    std::cout << "Order response: " << response << std::endl;
    return true;
}

额外注意事项

  • 令牌获取方式:Deribit官方要求用POST请求获取认证令牌,GET方式存在安全风险,且可能导致令牌权限异常,建议改用POST方式获取。
  • 合约名称验证:确保instrument_name是测试网存在的合约(比如BTC-PERPETUAL),避免传入不存在的合约名称。
  • API权限检查:在Deribit测试网后台确认你的API密钥已开启交易权限,无权限也会触发请求错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.16 22:13:19