cpp-httplib icon indicating copy to clipboard operation
cpp-httplib copied to clipboard

Couldn't resolve host name

Open azchatlanin opened this issue 1 year ago • 0 comments

There are two docker containers. Both are on the same network. When I make a post request from the first to the second, everything is fine. But if the second container is disabled, the first one crashed during the execution of the post request. (exited with code 0)

When I do the same thing, but using curl. curl throw an exception:

I would like to be able to intercept this exception, and not completely crushed the docker.

services:
  service_1:
    image: image
    container_name: service_1
    environment:
     - USER_UID=1000
     - USER_GID=1000
       #restart: always
    networks:
      - service_net

networks:
  service_net:
    external: true
// base request with httplib
httplib::Client m_cli { "service_2:5000" };


httplib::Headers headers = {
  { "Content-Type", "application/json" }
};

try
{
   auto r = m_cli.Post("/", headers, payload.dump(), "application/json" );
   log(r->status);
}
catch(...)
{
   log("error");
}
// dont catch error
// docker container exited with code 0

// base curl
CURL *curl;
CURLcode res;

curl = curl_easy_init();
if (curl) {
  curl_easy_setopt(curl, CURLOPT_URL, "service_2:5000");
      
  struct curl_slist *headers = NULL;
  headers = curl_slist_append(headers, "Content-Type: application/json");
  curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
  curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "param1=value1&param2=value2");

  res = curl_easy_perform(curl);
  if (res != CURLE_OK)                        <--- here i can catch error. docker container dont exited
  { 
    std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl;
  }

  curl_easy_cleanup(curl);
  curl_slist_free_all(headers);
}

azchatlanin avatar Jul 19 '24 09:07 azchatlanin