curl / Mailing Lists / curl-library / Single Mail
Buy commercial curl support from WolfSSL. We help you work out your issues, debug your libcurl applications, use the API, port to new platforms, add new features and more. With a team lead by the curl founder himself.

Problem trying to use Curl in C++ to get http response string after sending http string to server.

From: José Fernández via curl-library <curl-library_at_cool.haxx.se>
Date: Fri, 25 Sep 2020 16:01:08 +0000 (UTC)

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?
Thanks and regards,
José Fernańdez
----------------------------------------------------------------------------------------------------------------
SOURCE CODE:
----------------------------------------------------------------------------------------------------------------
 
#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;
}
 

-------------------------------------------------------------------
Unsubscribe: https://cool.haxx.se/list/listinfo/curl-library
Etiquette: https://curl.haxx.se/mail/etiquette.html
Received on 2020-09-25