I am getting this error deterministically every time I run the code when I am able to successfully query the site Postman and even using the binary curl command that is a result of the manual libcurl build described below.
This is the curl command that I am using (I am using <my.website> in place of the actual host and in place of the actual correct token):
PS> cd C:curl-8.8.0buildslibcurl-vc-x86-release-static-ipv6-sspi-schannelbin
PS> ./curl --header "Authorization: Bearer <token>" https://<my.website>/api/test
… and I am getting back the expected:
Hello back
However, when I try the same in my C++ x86 project in Visual Studio 2022 like this:
#include <curl/curl.h>
#include <iostream>
void test(CURL* curl) {
// setup
const char* url = "https://<my.website>/api/test";
curl_easy_setopt(curl, CURLOPT_URL, url);
struct curl_slist* headers = NULL;
headers = curl_slist_append(headers, "Authorization: Bearer <token>");
setres = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
// request
CURLcode res;
res = curl_easy_perform(curl);
std::cerr << "RESULT: " << res << std::endl;
// request result checking
if (res != CURLE_OK) {
std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl;
} else {
std::cout << "POST request sent successfully!" << std::endl;
}
// cleanup
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
}
int main() {
// global initialization
CURL * curl;
curl_global_init(CURL_GLOBAL_SSL);
curl = curl_easy_init();
if (curl) {
test(curl);
} else {
std::cerr << "INIT FAILED" << std::endl;
}
// global cleanup
curl_global_cleanup();
}
… I get the following output:
* getaddrinfo() thread failed to start
* Could not resolve host: <my.website>
* Closing connection
RESULT: 6
curl_easy_perform() failed: Couldn't resolve host name
(the <my.website> is again just a placeholder; the real output contains the actual website, of course)
This problem appears immediately with basically zero waiting time (non-zero waiting times may indicate DNS problems acccording to some answers I found).
I have built libcurl “8.8.0 Schannel WinIDN” on my Windows 11 in a x86 mode (using Developer Command Prompt for VS 2022). This created the C:curl-8.8.0buildslibcurl-vc-x86-release-static-ipv6-sspi-schannel
with the include
, lib
and bin
subdirectories.
My project is constructed as a x86 project with the following options in properties:
C/C++ / General / Additional Include Directories: C:curl-8.8.0buildslibcurl-vc-x86-release-static-ipv6-sspi-schannelinclude
C/C++ / Preprocessor / Preprocessor Definitions: CURL_STATICLIB;WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)
Linker / General / Additional Library Directories: C:curl-8.8.0buildslibcurl-vc-x86-release-static-ipv6-sspi-schannellib
Linker / Input / Additional Dependencies: libcurl_a.lib;Ws2_32.lib;Wldap32.lib;Crypt32.lib;Normaliz.lib;$(CoreLibraryDependencies);%(AdditionalDependencies)
While trying to figure out what’s wrong, I have read this post getaddrinfo thread failed to start #6495 which recommended to execute also the following:
curl_version_info_data* ver = curl_version_info(CURLVERSION_NOW);
std::cerr << "VERSION: " << curl_version() << std::endl;
if (!(ver->features & CURL_VERSION_ASYNCHDNS)) {
std::cerr << "SYNCHRONOUS" << std::endl;
} else if (!ver->age || ver->ares_num) {
std::cerr << "ARES" << std::endl;
} else {
std::cerr << "THREADED" << std::endl;
}
… which outputs:
VERSION: libcurl/8.8.0 Schannel WinIDN
THREADED
Which is what I would expect.
Can someone please tell me what I am doing wrong?