Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions docs-src/pages/en/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
title: "cpp-httplib"
order: 0
---

[cpp-httplib](https://github.com/yhirose/cpp-httplib) is an HTTP/HTTPS library for C++. Just copy a single header file, [`httplib.h`](https://github.com/yhirose/cpp-httplib/raw/refs/tags/latest/httplib.h), and you're ready to go.

When you need a quick HTTP server or client in C++, you want something that just works. That's exactly why I built cpp-httplib. You can start writing both servers and clients in just a few lines of code.

The API uses a lambda-based design that feels natural. It runs anywhere you have a C++11 or later compiler. Windows, macOS, Linux — use whatever environment you already have.

HTTPS works too. Just link OpenSSL or mbedTLS, and both server and client gain TLS support. Content-Encoding (gzip, Brotli, etc.), file uploads, and other features you actually need in real-world development are all included. WebSocket is also supported.

Under the hood, it uses blocking I/O with a thread pool. It's not built for handling massive numbers of simultaneous connections. But for API servers, embedded HTTP in tools, mock servers for testing, and many other use cases, it delivers solid performance.

"Solve today's problem, today." That's the kind of simplicity cpp-httplib aims for.

## Documentation

- [A Tour of cpp-httplib](tour/index) — A step-by-step tutorial covering the basics. Start here if you're new
- [Cookbook](cookbook/index) — A collection of recipes organized by topic. Jump to whatever you need
88 changes: 88 additions & 0 deletions docs-src/pages/en/tour/01-getting-started.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
---
title: "Getting Started"
order: 1
---

All you need to get started with cpp-httplib is `httplib.h` and a C++ compiler. Let's download the file and get a Hello World server running.

## Getting httplib.h

You can download it directly from GitHub. Always use the latest version.

```sh
curl -LO https://github.com/yhirose/cpp-httplib/raw/refs/tags/latest/httplib.h
```

Place the downloaded `httplib.h` in your project directory and you're good to go.

## Setting Up Your Compiler

| OS | Development Environment | Setup |
| -- | ----------------------- | ----- |
| macOS | Apple Clang | Xcode Command Line Tools (`xcode-select --install`) |
| Ubuntu | clang++ or g++ | `apt install clang` or `apt install g++` |
| Windows | MSVC | Visual Studio 2022 or later (install with C++ components) |

## Hello World Server

Save the following code as `server.cpp`.

```cpp
#include "httplib.h"

int main() {
httplib::Server svr;

svr.Get("/", [](const httplib::Request&, httplib::Response& res) {
res.set_content("Hello, World!", "text/plain");
});

svr.listen("0.0.0.0", 8080);
}
```

In just a few lines, you have a server that responds to HTTP requests.

## Compiling and Running

The sample code in this tutorial is written in C++17 for cleaner, more concise code. cpp-httplib itself can compile with C++11 as well.

```sh
# macOS
clang++ -std=c++17 -o server server.cpp

# Linux
# `-pthread`: cpp-httplib uses threads internally
clang++ -std=c++17 -pthread -o server server.cpp

# Windows (Developer Command Prompt)
# `/EHsc`: Enable C++ exception handling
cl /EHsc /std:c++17 server.cpp
```

Once it compiles, run it.

```sh
# macOS / Linux
./server

# Windows
server.exe
```

Open `http://localhost:8080` in your browser. If you see "Hello, World!", you're all set.

You can also verify with `curl`.

```sh
curl http://localhost:8080/
# Hello, World!
```

To stop the server, press `Ctrl+C` in your terminal.

## Next Steps

Now you know the basics of running a server. Next, let's look at the client side. cpp-httplib also comes with HTTP client functionality.

**Next:** [Basic Client](../02-basic-client)
266 changes: 266 additions & 0 deletions docs-src/pages/en/tour/02-basic-client.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,266 @@
---
title: "Basic Client"
order: 2
---

cpp-httplib isn't just for servers -- it also comes with a full HTTP client. Let's use `httplib::Client` to send GET and POST requests.

## Preparing a Test Server

To try out the client, you need a server that accepts requests. Save the following code, then compile and run it the same way you did in the previous chapter. We'll cover the server details in the next chapter.

```cpp
#include "httplib.h"
#include <iostream>

int main() {
httplib::Server svr;

svr.Get("/hi", [](const auto &, auto &res) {
res.set_content("Hello!", "text/plain");
});

svr.Get("/search", [](const auto &req, auto &res) {
auto q = req.get_param_value("q");
res.set_content("Query: " + q, "text/plain");
});

svr.Post("/post", [](const auto &req, auto &res) {
res.set_content(req.body, "text/plain");
});

svr.Post("/submit", [](const auto &req, auto &res) {
std::string result;
for (auto &[key, val] : req.params) {
result += key + " = " + val + "\n";
}
res.set_content(result, "text/plain");
});

svr.Post("/upload", [](const auto &req, auto &res) {
auto f = req.form.get_file("file");
auto content = f.filename + " (" + std::to_string(f.content.size()) + " bytes)";
res.set_content(content, "text/plain");
});

svr.Get("/users/:id", [](const auto &req, auto &res) {
auto id = req.path_params.at("id");
res.set_content("User ID: " + id, "text/plain");
});

svr.Get(R"(/files/(\d+))", [](const auto &req, auto &res) {
auto id = req.matches[1];
res.set_content("File ID: " + std::string(id), "text/plain");
});

std::cout << "Listening on port 8080..." << std::endl;
svr.listen("0.0.0.0", 8080);
}
```

## GET Request

Once the server is running, open a separate terminal and give it a try. Let's start with the simplest GET request.

```cpp
#include "httplib.h"
#include <iostream>

int main() {
httplib::Client cli("http://localhost:8080");

auto res = cli.Get("/hi");
if (res) {
std::cout << res->status << std::endl; // 200
std::cout << res->body << std::endl; // Hello!
}
}
```

Pass the server address to the `httplib::Client` constructor, then call `Get()` to send a request. You can retrieve the status code and body from the returned `res`.

Here's the equivalent `curl` command.

```sh
curl http://localhost:8080/hi
# Hello!
```

## Checking the Response

A response contains header information in addition to the status code and body.

```cpp
auto res = cli.Get("/hi");
if (res) {
// Status code
std::cout << res->status << std::endl; // 200

// Body
std::cout << res->body << std::endl; // Hello!

// Headers
std::cout << res->get_header_value("Content-Type") << std::endl; // text/plain
}
```

`res->body` is a `std::string`, so if you want to parse a JSON response, you can pass it directly to a JSON library like [nlohmann/json](https://github.com/nlohmann/json).

## Query Parameters

To add query parameters to a GET request, you can either write them directly in the URL or use `httplib::Params`.

```cpp
auto res = cli.Get("/search", httplib::Params{{"q", "cpp-httplib"}});
if (res) {
std::cout << res->body << std::endl; // Query: cpp-httplib
}
```

`httplib::Params` automatically URL-encodes special characters for you.

```sh
curl "http://localhost:8080/search?q=cpp-httplib"
# Query: cpp-httplib
```

## Path Parameters

When values are embedded directly in the URL path, no special client API is needed. Just pass the path to `Get()` as-is.

```cpp
auto res = cli.Get("/users/42");
if (res) {
std::cout << res->body << std::endl; // User ID: 42
}
```

```sh
curl http://localhost:8080/users/42
# User ID: 42
```

The test server also has a `/files/(\d+)` route that uses a regex to accept numeric IDs only.

```cpp
auto res = cli.Get("/files/42");
if (res) {
std::cout << res->body << std::endl; // File ID: 42
}
```

```sh
curl http://localhost:8080/files/42
# File ID: 42
```

Pass a non-numeric ID like `/files/abc` and you'll get a 404. We'll cover how that works in the next chapter.

## Request Headers

To add custom HTTP headers, pass an `httplib::Headers` object. This works with both `Get()` and `Post()`.

```cpp
auto res = cli.Get("/hi", httplib::Headers{
{"Authorization", "Bearer my-token"}
});
```

```sh
curl -H "Authorization: Bearer my-token" http://localhost:8080/hi
```

## POST Request

Let's POST some text data. Pass the body as the second argument to `Post()` and the Content-Type as the third.

```cpp
auto res = cli.Post("/post", "Hello, Server!", "text/plain");
if (res) {
std::cout << res->status << std::endl; // 200
std::cout << res->body << std::endl; // Hello, Server!
}
```

The test server's `/post` endpoint echoes the body back, so you get the same string you sent.

```sh
curl -X POST -H "Content-Type: text/plain" -d "Hello, Server!" http://localhost:8080/post
# Hello, Server!
```

## Sending Form Data

You can send key-value pairs just like an HTML form. Use `httplib::Params` for this.

```cpp
auto res = cli.Post("/submit", httplib::Params{
{"name", "Alice"},
{"age", "30"}
});
if (res) {
std::cout << res->body << std::endl;
// age = 30
// name = Alice
}
```

This sends the data in `application/x-www-form-urlencoded` format.

```sh
curl -X POST -d "name=Alice&age=30" http://localhost:8080/submit
```

## POSTing a File

To upload a file, use `httplib::UploadFormDataItems` to send it as multipart form data.

```cpp
auto res = cli.Post("/upload", httplib::UploadFormDataItems{
{"file", "Hello, File!", "hello.txt", "text/plain"}
});
if (res) {
std::cout << res->body << std::endl; // hello.txt (12 bytes)
}
```

Each element in `UploadFormDataItems` has four fields: `{name, content, filename, content_type}`.

```sh
curl -F "file=Hello, File!;filename=hello.txt;type=text/plain" http://localhost:8080/upload
```

## Error Handling

Network communication can fail -- the server might not be reachable. Always check whether `res` is valid.

```cpp
httplib::Client cli("http://localhost:9999"); // Non-existent port
auto res = cli.Get("/hi");

if (!res) {
// Connection error
std::cout << "Error: " << httplib::to_string(res.error()) << std::endl;
// Error: Connection
return 1;
}

// If we reach here, we have a response
if (res->status != 200) {
std::cout << "HTTP Error: " << res->status << std::endl;
return 1;
}

std::cout << res->body << std::endl;
```

There are two levels of errors.

- **Connection error**: The client couldn't reach the server. `res` evaluates to false, and you can call `res.error()` to find out what went wrong.
- **HTTP error**: The server returned an error status (404, 500, etc.). `res` evaluates to true, but you need to check `res->status`.

## Next Steps

Now you know how to send requests from a client. Next, let's take a closer look at the server side. We'll dig into routing, path parameters, and more.

**Next:** [Basic Server](../03-basic-server)
Loading