首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将JSON数据发送到REST API?

如何将JSON数据发送到REST API?
EN

Stack Overflow用户
提问于 2019-06-19 00:12:55
回答 1查看 397关注 0票数 0

我在C++中使用libcurl和WooCommerce REST API安装了WooCommerce插件,并将数据发送到Wordpress站点。数据似乎已经发送,但网站上没有显示预期的结果。它的目的是更新(修改)产品。我的代码基于here的WooCommerce文档。

我已经设法从程序中获得了一个单独的txt文件中的CURLOPT_VERBOSE文本。

下面是我使用cURL编写的C++代码:

代码语言:javascript
复制
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
    }

我真的把在互联网上找到的每一个头文件都放在我的代码中,看起来与我想要完成的内容相关。以下是返回的调试文本:

代码语言:javascript
复制
* 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

我从原文中去掉了一些多余的部分,并保留了我认为是主要信息的部分。似乎我的JSON数据实际上被发送到了服务器,但是预期的结果并没有显示在我的网站上(一个产品应该被修改,但实际上没有)。这段代码有没有可能是错误的?或者问题出在服务器端?因为我很少使用官方文档中提到的东西。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-19 00:33:03

看起来你的有效载荷已经关闭了。id部分是多余的,因为您已经通过URL指定了要更新的产品,所以您可以删除它。此外,您正在尝试错误地设置价格。根据REST文档,您需要使用regular_price属性而不是price (price是只读的)。正确的有效负载应如下所示:

代码语言:javascript
复制
{
    name: 'My product name',
    description: 'my product description',
    regular_price' : '3.50',
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56653210

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档