简体   繁体   English

带有 cURL + JSON Lib 的 C++ POST 请求

[英]C++ POST request with cURL + JSON Lib

I would like to do a POST request using cURL .我想使用cURL做一个POST请求。 I am using this (github:nlohmann/json) library to handle my JSON object creation.我正在使用这个(github:nlohmann/json) 库来处理我的JSON对象创建。 I receive the HTTP 200 Response , but the POST data is not being appended.我收到HTTP 200 Response ,但未附加POST数据。

When calling std::cout<< json_data.dump() << std::endl;当调用std::cout<< json_data.dump() << std::endl; I receive a well-formed JSON .我收到一个格式良好的JSON

{
    "a": [
        {
            "c": "0",
            "d": "0",
            "e": "0",
            "f": "0",
            "g": "1506961983",
            "h": "1506961986",
            "i": "3"
        },
        {
            "c": "1",
            "d": "2",
            "e": "1",
            "f": "1",
            "g": "1506961987",
            "h": "1506961991",
            "i": "4"
        }
    ],
    "b": "test"
}

I use this to append my data.我用它来附加我的数据。

   struct curl_slist *headers=NULL; 
   headers = curl_slist_append(headers, "Accept: application/json");  
   headers = curl_slist_append(headers, "Content-Type: application/json");
   headers = curl_slist_append(headers, "charset: utf-8"); 
   curl_easy_setopt(curl, CURLOPT_POSTFIELDS,json_data.dump().c_str());

Documentation curl_easy_setopt documentation文档curl_easy_setopt 文档

If I look into my AWS logs.如果我查看我的 AWS 日志。 It says:它说:

{
  "format": "json",
  "payload": 5,
  "qos": 0,
  "timestamp": 1506961394810,
  "topic": "test_topic"
}

Why is it showing the value of 5 instead of my JSON object?为什么它显示的是5而不是我的JSON对象的值?

Thanks for help, if anyone knows why.感谢您的帮助,如果有人知道原因。

On this line:在这一行:

curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json_data.dump().c_str());

The string object returned by dump() is temporary and destroyed when curl_easy_setopt() exits, thus leaving CURLOPT_POSTFIELDS with a dangling pointer that may or may not still be pointing to the JSON data in memory by the time libCURL tries to post it. dump()返回的字符串对象是临时的,并在curl_easy_setopt()退出时被销毁,从而使CURLOPT_POSTFIELDS带有一个悬空指针,在 libCURL 尝试发布它CURLOPT_POSTFIELDS ,该指针可能仍然指向内存中的 JSON 数据,也可能不指向它。

Per the CURLOPT_POSTFIELDS documentation :根据CURLOPT_POSTFIELDS文档

The data pointed to is NOT copied by the library: as a consequence, it must be preserved by the calling application until the associated transfer finishes.指向的数据不会被库复制:因此,它必须由调用应用程序保留,直到关联的传输完成。 This behaviour can be changed (so libcurl does copy the data) by setting the CURLOPT_COPYPOSTFIELDS option.可以通过设置CURLOPT_COPYPOSTFIELDS选项来更改此行为(因此 libcurl 会复制数据)。

So, you need to either:因此,您需要:

  • change CURLOPT_POSTFIELDS to CURLOPT_COPYPOSTFIELDS :CURLOPT_POSTFIELDS更改为CURLOPT_COPYPOSTFIELDS

     curl_easy_setopt(curl, CURLOPT_COPYPOSTFIELDS, json_data.dump().c_str());
  • save the result of json_data.dump() to a local variable that does not go out of scope until after curl_easy_perform() exits:json_data.dump()的结果保存到一个局部变量中,该变量在curl_easy_perform()退出之前不会超出范围:

     std::string json = json_data.dump(); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json.c_str()); ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM