-1
curl_easy_setopt(curl, CURLOPT_URL, "127.0.0.1:8081/get.php");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS,"pulse=70 & temp=35" );

this above code run successfully but when I pass this

int pulsedata = 70;
int tempdata  = 35;

curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "pulse=pulsedata & temp = tempdata");

when I run this above line it give me error how can I pass this pulsedata and tempdata ??

2 Answers2

0

You can't use variable in strings like that, you have to format the string.

A possible C++ solution might be to use std::ostringstream like this:

std::ostringstream os;
os << "pulse=" << pulsedata << "&temp=" << tempdata;
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, os.str().c_sr());

With this solution, the std::ostringstream object (os in my example) needs to be alive until the CURL calls are all done.


Also note that the query-string I construct does not contain any spaces.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

A possible C solution:

char sendbuffer[100];
snprintf(sendbuffer, sizeof(sendbuffer), "pulse=%d&temp=%d", pulsedate, tempdata);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, sendbuffer);
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115