JJ007
JJ007

Reputation: 11

Deribit test API post requests failure

I am trying to hit this Deribit test API to place an order. I am facing error while sending POST requests using CURL. I have generated the auth token. This also I did via GET as POST was not working.

// Function to place an order on Deribit
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;
    }

    // failed: SSL peer certificate or SSH remote key was not OK
    curl_easy_setopt(curl, CURLOPT_CAINFO, "C:/Program Files/curl/cacert.pem");

    std::string order_url = "https://test.deribit.com/api/v2/private/buy";
    json payload = {
        {"instrument_name", instrument_name},
        {"amount", amount},
        {"price", "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_easy_cleanup(curl);
        return false;
    }

    curl_easy_cleanup(curl);

    // Print the JSON response from the order
    std::cout << "Order response: " << response << std::endl;
    return true;
}

I am getting this as repsonse:

Order response: {"jsonrpc":"2.0","error":{"message":"bad_request","code":11050},"testnet":false,"usIn":1729933493033379,"usOut":1729933493033421,"usDiff":42}

Any idea why this is happening, or if I am doing something wrong.

Upvotes: 1

Views: 134

Answers (0)

Related Questions