首页 > 解决方案 > C++ 发送形成和发送 JSON 结构并使用 CurlLib 发布

问题描述

当它只是一个带有“content = blahblah”的标准字符串时,这有效

所以我有我的 JSON 结构

{
  "content": null,
  "embeds": [
    {
      "title": "Login",
      "description": "Key = null",
      "color": null
    }
  ]
}

我想我已经正确地将它放入 C++ JSON

json data;
    data = R"(
        {
  "content": null,
  "embeds": [
    {
      "title": "Login",
      "description": "Key = null",
      "color": null
    }
  ]
}
    )"_json;

我有我的 curl 代码,但这会引发异常。

if (curl) {
        //Set URL that will be posted to
        curl_easy_setopt(curl, CURLOPT_URL, webhookURL);
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
        //Set post data
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.dump().data());
        //Get result
        res = curl_easy_perform(curl);
        if (res != CURLE_OK) {
            std::cout << curl_easy_strerror(res) << std::endl;
            curl_easy_cleanup(curl);
        }
    }
    curl_global_cleanup();

我再次更改了 cpp json,但我得到了一个不同的异常异常:

{"message": "Cannot send an empty message", "code": 50006}

请帮忙,谢谢

标签: c++jsoncurl

解决方案


推荐阅读