ccurllibcurl

How can I write equivalent libcurl code for a curl command with -d option and -X PUT option?


I'm tring to implement payhip Software License Keys usage API.

I've succeeded in issuing the following curl command;

curl https://payhip.com/api/v1/license/usage -d "product_link=MY_PRODUCT" -d "license_key=MY_KEY" -X PUT --header "payhip-api-key: API_KEY_HERE"

Then, I'm writing production code in c with libcurl library as follows;

   // curl
    CURL *curl = curl_easy_init();
    curl_easy_setopt(curl, CURLOPT_URL, "https://payhip.com/api/v1/license/usage");
    struct curl_slist *list = NULL;
    list = curl_slist_append(list, "API_KEY_HERE");
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
    curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "product_link=MY_PRODUCT");
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "license_key=MY_KEY");
    CURLcode res = curl_easy_perform(curl);

The curl_easy_perform(curl) returned 0, but the response was HTTP/2 400 and the response body said

{"code":"required_parameters","message":"Missing or empty required parameters","success":false}

How can I write collect put parameters in curl_easy_setopt()?


Solution

  • I've found the solution as follows:

    curl_easy_setopt(curl, CURLOPT_POSTFIELDS,"product_link=MY_PRODUCT&license_key=MY_KEY");
    // curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "product_link=MY_PRODUCT");
    // curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "license_key=MY_KEY");
    

    This answer is so informative for this question as well.