Search code examples
c++curlwoocommercelibcurlput

How to send JSON data to a REST API?


I'm sending data to a Wordpress site with the WooCommerce plugin installed using libcurl in C++ and the WooCommerce REST API. The data seems to get sent but the expected result is not shown on the website. The purpose of it is to update (modify) the product. My code is based on the WooCommerce documentation, found here.

I have managed to get the CURLOPT_VERBOSE text from the program in a separate txt file.

Here is my C++ code using cURL :

std::string URL = main_domain + "wp-json/wc/v3/products/" + product_id + "?consumer_key=" + consumer_key + "&consumer_secret=" + consumer_secret;
    curl_slist* headers = NULL;
    headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
    headers = curl_slist_append(headers, "Accept:application/json");
    headers = curl_slist_append(headers, "Content-Type:application/json");
    headers = curl_slist_append(headers, "charsets: utf-8");

    // log file
    FILE* filep = fopen("logfichier.txt", "w");

    std::string toUpdate = "{\"id\":\"" + product_id + ",\"name\":\"" + product_name + "\",\"description\":\"" + product_description + "\",\"price\":\"" + product_price + "\"}";

    curl_global_init(CURL_GLOBAL_ALL);
    curl = curl_easy_init();

    if (curl) {
        readBuffer = "";
        curl_easy_setopt(curl, CURLOPT_URL, URL.c_str());
        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
        curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT"); 
        curl_easy_setopt(curl, CURLOPT_POST, 1);
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, toUpdate.c_str());
        curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, toUpdate.length());
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
        curl_easy_setopt(curl, CURLOPT_VERBOSE, true);
        curl_easy_setopt(curl, CURLOPT_STDERR, filep);
        res = curl_easy_perform(curl);

        // Check for errors
        if (res != CURLE_OK) {
            // error handling and cleanup           
        }
        else {
            // code and cleanup
        }

    }
    else {
        // error handling and cleanup
    }

I've literally put every header found on the internet that seemed relevant to what i'm trying to acomplish in my code. Here is the returned debug text :

* STATE: INIT => CONNECT handle 0x10870278; line 1428 (connection #-5000)
* Added connection 0. The cache now contains 1 members
* STATE: CONNECT => WAITRESOLVE handle 0x10870278; line 1464 (connection #0)
*   Trying 192.XX.XX.XX...
* TCP_NODELAY set
* STATE: WAITRESOLVE => WAITCONNECT handle 0x10870278; line 1545 (connection #0)
* Connected to mywebsite.com (192.XX.XX.XX) port 443 (#0)
* STATE: WAITCONNECT => SENDPROTOCONNECT handle 0x10870278; line 1599 (connection #0)
* Marked for [keep alive]: HTTP default
* schannel: SSL/TLS connection with mywebsite.com port 443 (step 1/3)
* schannel: checking server certificate revocation
* schannel: sending initial handshake data: sending 176 bytes...

// (here was just a bunch of connexion attemps log text...)

* schannel: SSL/TLS handshake complete
* schannel: SSL/TLS connection with mywebsite.com port 443 (step 3/3)
* schannel: stored credential handle in session cache
* STATE: PROTOCONNECT => DO handle 0x10870278; line 1634 (connection #0)
> PUT /wp-json/wc/v3/products/111867?consumer_key=(the actual key)&consumer_secret=(the actual secret) HTTP/1.1

Host: mywebsite.com

Transfer-Encoding: chunked

Accept:application/json

Content-Type:application/json

charsets: utf-8


4b

* upload completely sent off: 82 out of 75 bytes
* STATE: DO => DO_DONE handle 0x10870278; line 1696 (connection #0)
* STATE: DO_DONE => WAITPERFORM handle 0x10870278; line 1823 (connection #0)
* STATE: WAITPERFORM => PERFORM handle 0x10870278; line 1838 (connection #0)
* schannel: client wants to read 16384 bytes
* schannel: encdata_buffer resized 17408
* schannel: encrypted data buffer: offset 0 length 17408

// (a few decrypting data attempts...)

* schannel: decrypted data returned 536
* schannel: decrypted data buffer: offset 0 length 16384
* HTTP 1.1 or later with persistent connection, pipelining supported
< HTTP/1.1 200 OK

< Date: Tue, 18 Jun 2019 15:27:42 GMT

* Server Apache is not blacklisted
< Server: Apache

< X-Robots-Tag: noindex

< Link: <https://mywebsite.com/wp-json/>; rel="https://api.w.org/"

< X-Content-Type-Options: nosniff

< Access-Control-Expose-Headers: X-WP-Total, X-WP-TotalPages

< Access-Control-Allow-Headers: Authorization, Content-Type

< Expires: Wed, 11 Jan 1984 05:00:00 GMT

< Cache-Control: no-transform, no-cache, must-revalidate, max-age=0

< Allow: GET, POST, PUT, PATCH, DELETE

< Transfer-Encoding: chunked

< Content-Type: application/json; charset=UTF-8

< 

* schannel: client wants to read 16384 bytes
* schannel: encrypted data buffer: offset 835 length 17408

// (a few decrypting data attempts...)

* schannel: decrypted data returned 1986
* schannel: decrypted data buffer: offset 0 length 16384
* STATE: PERFORM => DONE handle 0x10870278; line 2011 (connection #0)
* multi_done
* Connection #0 to host axanti.info left intact

I took out a few redundant part from the original text and kept what I think is the main piece of information. It seems that my JSON data is actually sent to the server but the intended result doesn't show up on my website (a product should be modified but it's actually not). Is there any way this code could be wrong ? Or is the problem on the server side ? Because I litteraly apply the same stuff that is mentioned in the official documentation.


Solution

  • Looks like your payload if off. The id portion is redundant, as you're already specifying the product to update via the URL, so you can drop that. Additionally, you're attempting to set the price incorrectly. Per the REST docs, you need to use the regular_price attribute instead of price (price is read only). The proper payload should look like this:

    {
        name: 'My product name',
        description: 'my product description',
        regular_price' : '3.50',
    }