Dmitrii Petrov
Dmitrii Petrov

Reputation: 21

Poloniex API "Invalid command" c++ libcurl

I'm trying to solve it for many hours but nothing going better...

int main() {
    CURL *curl_handle = curl_easy_init();
    if(curl_handle) {
     
        string post_data="";
        struct curl_slist *headers=NULL;
        string command = "command=returnBalances&nonce=" + to_string(time(0));
        cout<<command<<endl;
        string Secret = "mySecretCode";
        string Sign = "Sign: "+ hmac::get_hmac(Secret, command, hmac::TypeHash::SHA512);
        cout<<Sign<<endl;
        post_data += command;
        headers = curl_slist_append(headers, "Content-Type: application/x-www-form-urlencoded");
        headers = curl_slist_append(headers, "Key: myKey");
        headers = curl_slist_append(headers, Sign.c_str());
        curl_easy_setopt(curl_handle, CURLOPT_URL, "https://poloniex.com/tradingApi");

        curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDSIZE, post_data.length()+1);
        curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDS, post_data.c_str());
        curl_easy_setopt(curl_handle, CURLOPT_HTTPHEADER, headers);
        curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYPEER, false);
        curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1L);

    }
    curl_easy_perform(curl_handle);
    curl_easy_cleanup(curl_handle);
      
    return 0;
}

This returns to me

{"error":"Invalid command."}

I checked hmac function result threw website and they are equal. Some people said that adding

"Content-Type: application/x-www-form-urlencoded"

can solve this, but not at this time.

Console output:

> POST /tradingApi HTTP/2
Host: poloniex.com
accept: */*
content-type: application/x-www-form-urlencoded
key: myKey
sign: resultOfHmacFunc
content-length: 40

Upvotes: 0

Views: 133

Answers (1)

Dmitrii Petrov
Dmitrii Petrov

Reputation: 21

Solution was to change from CURLOPT_POSTFIELDS to CURLOPT_COPYPOSTFIELDS

Upvotes: 0

Related Questions