lundi 2 octobre 2017

C++ POST request with cURL + JSON Lib

I would like to do a POST request using cURL. I am using this (github:nlohmann/json) library to handle my JSON object creation. I receive the HTTP 200 Response, but the POST data is not being appended.

When calling std::cout<< json_data.dump() << std::endl; I receive a well-formed 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, "charsets: utf-8"); 
   curl_easy_setopt(curl, CURLOPT_POSTFIELDS,json_data.dump().c_str());

Documentation curl_easy_setopt documentation

If I look into my AWS logs. 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?

Thanks for help, if anyone knows why.

Aucun commentaire:

Enregistrer un commentaire