HTTP
HTTP is the request-and-response protocol the web runs on. A C++ program is usually a client (libcurl, cpp-httplib, Boost.Beast) or a server (Beast, Crow, Drogon, httplib). The language has no net/http in the standard library.
Detailed Explanation
An HTTP/1.1 request is text: a method, a target, a version, headers, a blank line, an optional body. The response is a status line, headers, a blank line, a body. HTTP/2 multiplexes binary frames on one TLS connection. Your code almost never writes those bytes by hand. A library parses them and gives you a method, a path, and a buffer.
C++ does not ship this. You pick a library and you live with its threading and lifetime model.
libcurl is the portable C client. You set options, perform, and write callbacks. Wrap the easy handle in a unique_ptr with curl_easy_cleanup. It speaks HTTP/1.1, HTTP/2, proxies, and a dozen protocols.
cpp-httplib is a header-only C++11 client and server. httplib::Client cli("https://api.example.com"); auto res = cli.Get("/health");. Fine for tools and tests. Not a 100k-QPS server.
Boost.Beast sits on Asio. You own buffers, parsers, and the io_context. It is the right layer when you already have an Asio app or you need HTTP/1.1 on a custom transport.
Servers: Crow and Drogon are framework-shaped. Beast is a toolkit. httplib is a single-header server for admin ports.
TLS is not optional on the public internet. See the TLS page. HTTP without TLS is http:// and is fine on localhost and behind a proxy that terminates TLS.
HTTP is stateless. Cookies and bearer tokens travel on every request. Connection reuse (keep-alive, HTTP/2) is a library setting. Creating a new TCP+TLS session per call will dominate your latency.
Status codes and REST have their own pages. This page is the protocol and the C++ ecosystem.
Code Examples
#include <httplib.h>
#include <iostream>
int main() {
httplib::Client cli("http://127.0.0.1:8080");
cli.set_connection_timeout(2, 0);
auto res = cli.Get("/health");
if (!res) {
std::cout << "no connection\n";
return 1;
}
std::cout << res->status << '\n'; // e.g. 200
std::cout << res->body << '\n';
}GET /health HTTP/1.1
Host: 127.0.0.1:8080
Accept: */*
HTTP/1.1 200 OK
Content-Type: text/plain
Content-Length: 2
okCommon Gotchas
No standard HTTP type
example code that `#include <httplib.h>` needs that library. libcurl looks different. Do not assume a header exists on every platform.
Blocking `Get` on a GUI or ASIO thread
use async APIs or a worker. A hung peer freezes the thread.
Keep one client / one curl multi per process
handshake cost is the first request. Recreating the client per call wastes TLS.
HTTP/2 and HTTP/1.1 servers are not the same code path
test the protocol you ship.