1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
#include <iostream>
#include <curl/curl.h>
#define HOST_NAME "http://localhost:8080/api/v1/"
#define ACCESS_TOKEN "MY_ACCESS_TOKEN"
#define HOST_SUFFIX "/telemetry"
#define JSON_DATA "{\"temperature\": 25}"
int main()
{
CURL *curl;
CURLcode res;
curl_global_init(CURL_GLOBAL_ALL);
/* get a curl handle */
curl = curl_easy_init();
if(curl) {
std::string myURL = std::string(HOST_NAME) += std::string(ACCESS_TOKEN) += std::string(HOST_SUFFIX); // build the url
curl_easy_setopt(curl, CURLOPT_URL, myURL.c_str() ); // set the url
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Content-Type: application/json"); // json header
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
/* Now specify the POST data */
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, JSON_DATA);
/* Perform the request, res will get the return code *
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return 0;
return 0;
}
|