c++ - 如何将 JSON 数据发送到 REST API?
问题描述
我正在使用 C++ 中的 libcurl 和 WooCommerce REST API 安装的 WooCommerce 插件将数据发送到 Wordpress 站点。数据似乎已发送,但网站上未显示预期结果。它的目的是更新(修改)产品。我的代码基于 WooCommerce 文档,可在此处找到。
我已经设法CURLOPT_VERBOSE
从一个单独的 txt 文件中的程序中获取文本。
这是我使用 cURL 的 C++ 代码:
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
}
我已经把在互联网上找到的每个标题都放在了似乎与我试图在我的代码中完成的内容相关的地方。这是返回的调试文本:
* 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 数据实际上已发送到服务器,但预期的结果并未显示在我的网站上(应该修改产品,但实际上没有)。这段代码有什么错误的吗?还是服务器端的问题?因为我几乎应用了官方文档中提到的相同内容。
解决方案
如果关闭,看起来像您的有效载荷。该id
部分是多余的,因为您已经通过 URL 指定要更新的产品,因此您可以删除它。此外,您试图错误地设置价格。根据 REST 文档,您需要使用regular_price
属性而不是price
(price
是只读的)。正确的有效载荷应如下所示:
{
name: 'My product name',
description: 'my product description',
regular_price' : '3.50',
}
推荐阅读
- java - 具有替代合并功能的合并排序的时间复杂度
- typescript - TypeScript:为什么这里有一个 `keyof` 可以解决这个问题
- python - 列表到嵌套列表和字典
- string - 在 Dart 中是否有任何用于检查空字符串和/或空字符串的较短版本?
- javascript - 本地主机上的 Nestjs(服务器)和 Vuejs(客户端)上的 CORS 阻塞获取功能
- javascript - .isAuthenticated() 总是假 || 护照本地策略
- php - 为什么HTML代码在while循环冷中与php结合不执行?
- python-3.x - 尝试使用 Python 在包含括号的路径中创建文件时出错
- html - 有没有办法正确使用 CSS 在 div 元素内缩小图像并保留其比例?
- java - 在代码中解密 AES 加密的授权令牌方法时,Jar 崩溃。hs_err _pid 文件提到 arrayof_jbyte_fill