-
Notifications
You must be signed in to change notification settings - Fork 0
/
curl_example.c
51 lines (42 loc) · 1.51 KB
/
curl_example.c
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
#include <stdio.h>
#include <curl/curl.h>
#include <time.h>
int main(void)
{
CURL *curl;
CURLcode res;
struct curl_slist *headers = NULL;
char url[] = "http://localhost:8000/distance_data";
double distance = 10.5; // replace with the appropriate distance value
// Initialize libcurl
curl = curl_easy_init();
if (curl) {
// Set the POST headers
headers = curl_slist_append(headers, "Content-Type: application/json");
time_t timestamp = time(NULL); // Get now timestamp to send the server
// THIS how to get data inside string using sprintf
// #####################
char post_data[100];
sprintf(post_data, "{\"distance\": %.2f, \"timestamp\": %ld}", distance, timestamp);
// #####################
/*
In case of wanting to send the ph data use this line instead
sprintf(post_data, "{\"ph\": %.2f, \"timestamp\": %ld}", phValue, timestamp);
*/
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_data);
// Set the URL and headers
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
// Perform the request
res = curl_easy_perform(curl);
// Cleanup
curl_easy_cleanup(curl);
curl_slist_free_all(headers);
// Check for errors
if (res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
return 1;
}
}
return 0;
}