HTTP clients
An HTTP client sends requests and reads responses. In C++ you choose a library: libcurl for portable power, cpp-httplib for small tools, Boost.Beast for Asio integration. There is no std::http.
Detailed Explanation
A client owns connections. Creating a new TCP+TLS session per request is the slowest thing you will do. Hold one CURL* multi handle, one httplib::Client, or one Beast connection pool for the life of the process (or the request-scoped pool, if you have many hosts).
libcurl. C API, every protocol, HTTP/2, proxies, cookies, mTLS. The easy interface is synchronous. The multi interface is event-loop friendly. Always set a timeout (CURLOPT_TIMEOUT_MS). Always check CURLE_OK. Wrap cleanup in unique_ptr. Write callbacks must copy bytes out; the buffer curl gives you dies after the callback.
cpp-httplib. Client("https://host") plus Get/Post. Timeouts are methods on the client. HTTPS needs OpenSSL (or the library's bundled option) at build time. Good for CLI tools and tests. The server class in the same header is not your public edge at scale.
Boost.Beast. You write the request message, async write, async read, parse. You own the executor. Use it when the rest of the process is already Asio. Do not start a Beast client from scratch for a three-line GET; use curl or httplib.
Common settings you must not forget:
- Timeouts (connect, overall).
- TLS verification (
CURLOPT_SSL_VERIFYPEERstays on). - Follow-redirect policy (and never https→http if you can help it).
User-Agent(some APIs require it).- Connection reuse.
Bodies: send JSON as a string with Content-Type: application/json. Receive into a string or a file stream. Huge downloads should stream; do not buffer a 2 GB body.
Thread safety: a libcurl easy handle is not shared across threads. httplib Client is not a lock-free shared object. Give each thread a handle or lock.
Code Examples
#include <cstddef>
#include <curl/curl.h>
#include <memory>
#include <string>
static size_t append(char* ptr, size_t size, size_t nmemb, void* user) {
auto* out = static_cast<std::string*>(user);
out->append(ptr, size * nmemb);
return size * nmemb;
}
int main() {
curl_global_init(CURL_GLOBAL_DEFAULT);
std::unique_ptr<CURL, decltype(&curl_easy_cleanup)>
easy(curl_easy_init(), &curl_easy_cleanup);
if (!easy) return 1;
std::string body;
curl_easy_setopt(easy.get(), CURLOPT_URL, "https://example.com/");
curl_easy_setopt(easy.get(), CURLOPT_WRITEFUNCTION, append);
curl_easy_setopt(easy.get(), CURLOPT_WRITEDATA, &body);
curl_easy_setopt(easy.get(), CURLOPT_TIMEOUT, 10L);
CURLcode rc = curl_easy_perform(easy.get());
curl_global_cleanup();
return rc == CURLE_OK ? 0 : 1;
}Common Gotchas
No timeout
a hung peer occupies a thread forever. Set one.
`SSL_VERIFYPEER=0`
you disabled TLS authenticity. Fix the CA store instead.
Sharing one easy handle across threads
curl documents this as unsupported. Use one handle per thread or the multi interface with a lock.
`curl_global_init` is process-wide
call it once before threads start, cleanup once at the end.