---
title: "Cpp簡易api教學"
url: https://geminixiang.com/posts/cpp-simple-api-tutorial/
description: "用 header-only 的 cpp-httplib 在 C++ 架設簡易 HTTP API，再用 nlohmann/json 回傳 JSON。"
published: 2022-08-27
category: "Programming"
tags: ["cpp", "api", "httplib"]
---

# Cpp簡易api教學

---

# 前言
近期在公司接觸到其他團隊的C++專案，遇到與其他語言(GO)/系統串接的情況。
有人提議將使用 stdin 讓兩個語言做溝通(實際效果也不錯，下次再分享)，
但習慣使用webapi的我，突然發覺自己不曾使用過cpp去架設過api...
於是就有了這篇
週末閒來無事，google了一下，找到 github 上擁有 7.6k stars 的 `cpp-httplib`。
引用時也很方便，直接將 `httplib.h` 拉進自己的專案內就可以使用，真舒爽!


# Library
## httplib  [🔗](https://github.com/yhirose/cpp-httplib)
> A C++11 single-file header-only cross platform HTTP/HTTPS library.
> It's extremely easy to setup. Just include the httplib.h file in your code!
> 
> NOTE: This library uses 'blocking' socket I/O. If you are looking for a library with 'non-blocking' socket I/O, this is not the one that you want.

開頭就提及，此 library 是 blocking socket I/O，對於 I/O 密集的 api 可能就不太適合了。

### 簡易範例
> NOTE: 記得建一個資料夾 `lib`，將 [httplib.h](https://raw.githubusercontent.com/yhirose/cpp-httplib/master/httplib.h) 放進去，這樣就可以使用了，真是方便呢!!

```c++
// main.cpp
#include <stdio.h>
#include <iostream>
#include "lib/httplib.h"
using namespace httplib;

void hello_world(const Request& req,Response& resp) {
  printf("%s [%s] %s, %s, %s %i \n", 
            ctime(&my_time),
            req.method.c_str(),
            req.path.c_str(),
            req.remote_addr.c_str(),
            req.version.c_str(),
            req.remote_port);
  resp.set_content("<html><h1>Hello world.</h1></html>","text/html");
}

int main() {
  Server svr;
  svr.Get("/api/hello", hello_world);
  svr.listen("0.0.0.0", 8000);
  system("pause");
  return 0;
}
```

輸入 `g++ -g main.cpp -o svr -lpthread && ./svr` 編譯並執行 

(如果沒安裝 g++)
```bash
sudo apt update
sudo apt install g++
```

安裝完成測試一下

```bash
g++ --version

>>> g++ (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
```

最後使用瀏覽器開啟 [localhost:8000/api/hello](http://localhost:8000/api/hello)
即可看到 Hello world.

---

## nlohmann/json  [🔗](https://github.com/nlohmann/json)
> based on c++11

### 使用範例
NOTE: 將 [json.hpp](https://github.com/nlohmann/json/releases/download/v3.11.2/json.hpp) 放進 `./lib` 當中，即可使用，也是很方便的~

```cpp
#include <lib/json.hpp>
using json = nlohmann::json;

void json_callback(const Request& req, Response& resp) {
  json result{
        {"msg", "ok"},
        {"zh-tw", "你好"},
        {"data", 123}};
  resp.set_content(result.dump(), "text/plain");
}

int main() {
  Server svr;
  svr.Get("/api/json", json_callback);
  svr.listen("0.0.0.0", 8000);
  system("pause");
  return 0;
}
```

最後使用瀏覽器開啟 [localhost:8000/api/json](http://localhost:8000/api/json)
即可看到 Hello world.


以上就是這次的簡易教學，相關code有放置在 [github-demo](https://github.com/geminixiang/cpp-webapi)，可以git clone 下來把玩。

---

## About the author

**Ying Xiang Zhao** (geminixiang) — DevOps Engineer, Taipei, Taiwan.

Ying Xiang Zhao（geminixiang）是住在台北的 DevOps 工程師，工作橫跨雲原生基礎設施、CI/CD、ML 模型部署與全端開發。這個部落格記錄 infra、資安與 AI coding agent 的實戰筆記；作品包含開源專案 mikan（把 AI coding agent 帶進 Slack / Telegram / Discord）與 tiny-agent（用四種語言實作的極簡 coding agent），瀏覽器內的影片編輯器 Studio，並經營旅遊分帳工具 SentriPay。

**Open to work** — DevOps Engineer / Platform Engineer / Site Reliability Engineer / AI Infrastructure Engineer · Taipei, Taiwan / Remote. Résumé: https://geminixiang.com/about/ (JSON: https://geminixiang.com/resume.json)

Topics: DevOps, Cloud Native, Kubernetes, Docker, Terraform, Google Cloud, CI/CD, Security, AI coding agents, ML model deployment, TypeScript, Python

Projects:
- [mikan](https://github.com/geminixiang/mikan) — Self-hosted AI coding agent for Slack, Telegram, Discord and GitHub with per-conversation sandboxes
- [tiny-agent](https://github.com/geminixiang/tiny-agent) — Minimal AI coding agents in TypeScript, Go, Python and Rust, plus a book on how they work
- [SentriPay](https://sentripay.com) — Travel expense splitter with multi-currency conversion and shareable, live-synced trip ledgers
- [Studio](https://studio.geminixiang.com) — Browser-based video editor built on WebCodecs

[About](https://geminixiang.com/about/) · [Résumé](https://geminixiang.com/about/) · [LinkedIn](https://linkedin.com/in/geminixiang/) · [GitHub](https://github.com/geminixiang) · [Twitter](https://x.com/geminixiang)
