Building Fast APIs with Ease: Introducing the cpp-httplib Server and Client
The cpp-httplib library is a powerful and practical tool for C++ developers, especially due to its header-only nature and comprehensive feature set.
Ease of Integration (Header-Only)
This is arguably the biggest advantage. As a header-only library, you don't need to compile and link separate library files. You just include the header (httplib.h), and you're ready to go! This drastically simplifies the build process and is excellent for rapid prototyping or integrating into existing C++ projects.
Versatility (Client & Server)
It offers both HTTP/HTTPS server and client functionalities. This means you can use one library to
Build RESTful APIs/Web Services (Server).
Consume External APIs/Services (Client).
Modern C++ and Performance
Being written in C++, it allows for the development of high-performance applications. It's built to be fast and efficient, which is crucial for network-intensive tasks.
Cross-Platform
It generally works well across different operating systems, which is a key requirement for most professional software development.
Adopting cpp-httplib is straightforward, thanks to its header-only design.
The simplest way is to download the httplib.h file directly from the GitHub repository (yhirose/cpp-httplib).
Place the downloaded httplib.h file in a location accessible to your compiler (like your project's include directory or the same folder as your source code).
Since it often relies on system libraries for network and SSL (like OpenSSL for HTTPS), you'll typically need to add specific compiler flags, especially when enabling SSL.
Example GCC/Clang Compile Command (for an HTTPS client, you often need openssl libraries)
g++ -std=c++11 main.cpp -o app -lssl -lcrypto -pthread
main.cpp is your source file.
-lssl and -lcrypto link against the OpenSSL libraries.
-pthread is often required for thread-safety and concurrent operations.
Here are simple, practical examples for both the server and client components.
This demonstrates creating a simple web server that responds to a GET request at the root path (/).
#include "httplib.h"
#include <iostream>
void run_server() {
// 1. Create a server instance
httplib::Server svr;
// 2. Define a GET route handler for the path "/"
svr.Get("/", [](const httplib::Request &req, httplib::Response &res) {
// Set the response body and status code (200 OK)
res.set_content("Hello, C++ World!", "text/plain");
});
// 3. Start the server on port 8080
std::cout << "Server listening on http://localhost:8080" << std::endl;
// The server runs in a blocking loop until stopped
if (!svr.listen("0.0.0.0", 8080)) {
std::cerr << "Could not start server!" << std::endl;
}
}
int main() {
run_server();
return 0;
}
To Test
After compiling and running, open your browser or use a tool like curl to visit http://localhost:8080.
This demonstrates making a GET request to an external server.
#include "httplib.h"
#include <iostream>
#include <string>
void run_client() {
// 1. Create a Client instance, specifying the host (protocol and domain)
httplib::Client cli("http://www.example.com");
// 2. Make a GET request to the path "/"
auto res = cli.Get("/");
// 3. Check the response
if (res && res->status == 200) {
// Successful response (200 OK)
std::cout << "Successfully fetched page. Body size: "
<< res->body.length() << " bytes" << std::endl;
// Output the first 200 characters of the body for a peek
std::cout << "\n--- Response Snippet ---\n"
<< res->body.substr(0, 200) << "..." << std::endl;
} else {
// Handle errors (e.g., connection failure, non-200 status)
int status = res ? res->status : -1;
std::cerr << "HTTP request failed! Status code: " << status << std::endl;
}
}
int main() {
run_client();
return 0;
}
This library is a fantastic choice when you need the speed of C++ but the simplicity of a modern, lightweight HTTP handler.