Using Curl in C++ to get http response string after sending http string to server.

In a C ++ program I need to send an http message using Curl to another machine where is running a
REST server written in C ++, and then collect the response from that server.

I am new to using Curl and have been looking at documentation and various examples on the web.

The source code of my program is the one attached below.

When my program sends the message to the server I get this error:
curl_easy_perform() failed to send message: Timeout was reached
But the server recives the message.

When the program tries to get the response from the server sends it always shows on the screen:
curl_easy_perform() failed to get answer: Timeout was reached
I have tried to increase the timeout and I get the same error.

I would appreciate help with this, as I am not sure the code I use to get the response from the server is correct.

Could you confirm me, please, if the source code I use to get the answer from the server after sending it a message is right?


SOURCE CODE IS:
---------------------------

#include <stdio.h>
#include <iostream>
#include <unistd.h>
#include <sstream>
#include <curl/curl.h>



size_t getAnswerFunction(void* ptr, size_t size, size_t nmemb, std::string* data) {
data->append((char*)ptr, size * nmemb);
return size * nmemb;
}



void sendDataAndGetAnswer(std::string data)
{
CURL *curl;
CURLcode res;
struct curl_slist *httpHeaders=NULL;

curl = curl_easy_init();
if (curl)
{
curl_global_init(CURL_GLOBAL_ALL);
curl_easy_setopt(curl, CURLOPT_URL, "http://192.168.0.100:15000");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str());

httpHeaders = curl_slist_append(httpHeaders, "MyProgram-Header");

curl_easy_setopt(curl, CURLOPT_HTTPHEADER, httpHeaders);
curl_easy_setopt(curl, CURLOPT_USERAGENT, "MyAgent/1.0");
curl_easy_setopt(curl, CURLOPT_POST, 1L);
curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, 1000);

// .........................................
// MyProgram sends data.
// This works right.
// .........................................

res = curl_easy_perform(curl);
if (res != CURLE_OK)
std::cout << "curl_easy_perform() failed to send message: " << curl_easy_strerror(res) << std::endl;


// .........................................
// MyProgram get answer.
// This does not work.
// .........................................

std::string response_string;
curl_easy_setopt(curl, CURLOPT_URL, "http://192.168.0.100:15000");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, getAnswerFunction);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response_string);
// getAnswerFunction waits at most 5000 ms.
// Afterwards, the flow of the program should continue even if there is no response.
curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, 5000);

res = curl_easy_perform(curl);
if (res == CURLE_OK)
std::cout << "The answer from server is: " << response_string;
else
std::cout << "curl_easy_perform() failed to get answer: " << curl_easy_strerror(res) << std::endl;


curl_easy_cleanup(curl);
curl_slist_free_all(httpHeaders);
}
}



int main(void)
{

while (1)
{
int valueVar = 0;
// Execute various statements and perform calculations.
// ...

// Calculate valueVar here.
// ...
valueVar = 5;

std::string msgToSend("CONTITION-IS-TRUE");
if (valueVar == 5)
sendDataAndGetAnswer(msgToSend);

sleep(10);
}


return 0;
}
Last edited on
What is your REST server saying while all this is going on?
Is it getting a connection?
Is it sending a response?

> getAnswerFunction
Use your debugger, does it get called at all?

> // MyProgram get answer.
You're re-using the same curl object you used to send with.
Perhaps fully cleanup and re-initialise your curl instance when switching between sending and receiving.

You absolutely need this tool when doing network work - https://www.wireshark.org/
Being able to see the actual traffic between the machines is a great help to debugging.

Oh, and format your code please.
https://www.cplusplus.com/articles/jEywvCM9/
Last edited on
Topic archived. No new replies allowed.