๐๐ป ์น๊ฐ๋ฐ์ด ์๋ ์ผ๋ฐ c++ ์ฑ ๊ฐ๋ฐ์ ์น์๋ฒ์ ํต์ ํ๋ ๋ฐฉ๋ฒ์ ๋ํ ์ค๋ช
์
๋๋ค.
This explains how to communicate with a web server when developing general C++ apps, not web development.
๐๐ป http,https,json ํ์ฑ์ ๋ํ ๋ถ๋ถ์ ๋ค๋ฃน๋๋ค.
This covers http, https, and JSON parsing.
๐๐ป ๋ฐฉํ๋ฒฝ ์ค์ ์ด๋ https์ค์ ๋ถ๋ถ์ ์ด์ ๊ฒ์๋ฌผ์ ์ฐธ๊ณ ํ์ธ์.
Please refer to the previous post for firewall and HTTPS settings.
๐๐ปHttp ์๋ฒ(MacOS์์ ํ
์คํธ)
HTTP server (tested on MacOS)
โ๏ธ ์ฝ๋ / code
#include "httplib.h"
#include <iostream>
int main() {
httplib::Server svr;
svr.Get("/", [](const httplib::Request&, httplib::Response& res) {
res.set_content("Hello HTTP", "text/plain");
});
std::cout << "Listening on http://0.0.0.0:5080\n";
svr.listen("0.0.0.0", 5080);
}
โ๏ธ ์ปดํ์ผ / Compiling
g++ -o http_server http_server.cpp -std=c++11 -pthread
โ๏ธ ์คํ / run
./http_server

๐๐ปHttp ํด๋ผ์ด์ธํธ / Http client
โ๏ธ ์ฝ๋ / code
#include "httplib.h"
#include <iostream>
int main() {
httplib::Client cli("http://127.0.0.1:5080");
if (auto res = cli.Get("/")) {
std::cout << "status: " << res->status << "\n";
std::cout << "body: " << res->body << "\n";
} else {
std::cerr << "request failed\n";
}
}
โ๏ธ ์ปดํ์ผ / Compiling
g++ -o http_client http_client.cpp -std=c++11 -pthread
โ๏ธ ์คํ / run
./http_client

๐๐ป Https ์๋ฒ(Linux์์ ํ
์คํธ)
HTTPS Server (Tested on Linux)
โญ๏ธ libssl-dev ์ค์น๋์ด ์์ด์ผ ํฉ๋๋ค
libssl-dev must be installed.
sudo apt install libssl-dev
โ๏ธ ์ฝ๋ / Code
#define CPPHTTPLIB_OPENSSL_SUPPORT
#include "../httplib.h"
#include <iostream>
int main() {
httplib::SSLServer svr("./fullchain.pem", "./privkey.pem");
if (!svr.is_valid()) {
std::cerr << "SSL server setup failed\n";
return 1;
}
svr.Get("/", [](const httplib::Request&, httplib::Response& res) {
res.set_content("<h1>Hello HTTPS</h1>", "text/html");
});
std::cout << "Listening on https://0.0.0.0:5080\n";
svr.listen("0.0.0.0", 5080);
}
โ๏ธ ์ปดํ์ผ / Compiling
g++ https_server.cpp -o https_server -std=c++11 -pthread -lssl -lcrypto
โ๏ธ ์คํ / Run
./https_server

๐๐ปHttps ํด๋ผ์ด์ธํธ / Https Client
โ๏ธ ์ฝ๋ / Code
#define CPPHTTPLIB_OPENSSL_SUPPORT
#include "../httplib.h"
#include <iostream>
int main() {
httplib::Client cli("https://yourhost.domain.org:5080");
if (auto res = cli.Get("/")) {
std::cout << res->status << "\n";
std::cout << res->body << "\n";
} else {
std::cerr << "request failed\n";
}
}
โ๏ธ ์ปดํ์ผ / Compiling
g++ https_client.cpp -o https_client -std=c++11 -pthread -lssl -lcrypto
โ๏ธ ์คํ / Run
./https_client

๐๐ป Https Json(Linux์์ ํ
์คํธ)
HTTPS JSON (Tested on Linux)
โ๏ธ Linux
sudo apt update
sudo apt install nlohmann-json3-dev
โ๏ธ ์ค์น ๊ฒฝ๋ก / Installation path
/usr/include/nlohmann/json.hpp
โ๏ธ MacOS
brew install nlohmann-json
โ๏ธ ์ค์น๊ฒฝ๋ก / Installation path
/opt/homebrew/include/nlohmann/json.hpp
โ๏ธ ์์ฑ๋ json.httpํ์ผ์ ์์
์ค์ธ ๋๋ ํ ๋ฆฌ๋ก ๋ณต์ฌํฉ๋๋ค.
Copy the generated json.http file to the working directory.
sudo cp /usr/include/nlohmann/json.hpp ./
โ๏ธ ์๋ฒ์ฝ๋ / Server Code
— server.cpp
#define CPPHTTPLIB_OPENSSL_SUPPORT
#include "../httplib.h"
#include "json.hpp"
#include <iostream>
using json = nlohmann::json;
int main() {
httplib::SSLServer svr("./fullchain.pem", "./privkey.pem");
if (!svr.is_valid()) {
std::cerr << "SSL server setup failed\n";
return 1;
}
svr.Get("/api", [](const httplib::Request&, httplib::Response& res) {
json j = {
{"ok", true},
{"msg", "hello"},
{"value", 123}
};
res.set_content(j.dump(), "application/json");
});
std::cout << "Listening on https://0.0.0.0:5080\n";
svr.listen("0.0.0.0", 5080);
}
โ๏ธ ํด๋ผ์ด์ธํธ ์ฝ๋ / Client Code
— client.cpp
#define CPPHTTPLIB_OPENSSL_SUPPORT
#include "../httplib.h"
#include "json.hpp"
#include <iostream>
using json = nlohmann::json;
int main() {
httplib::Client cli("https://yourhost.domain.org:5080");
if (auto res = cli.Get("/api")) {
json j = json::parse(res->body);
std::cout << "ok: " << j["ok"] << "\n";
std::cout << "msg: " << j["msg"] << "\n";
std::cout << "value: " << j["value"] << "\n";
} else {
std::cerr << "request failed\n";
}
}
โ๏ธ ์ปดํ์ผ / Compiling
— Linux
g++ -o server.cpp -std=c++11 -pthread -lssl -lcrypto
g++ -o client.cpp -std=c++11 -pthread -lssl -lcrypto
— MacOS
g++ server.cpp -o server -std=c++11 -pthread \
-I/opt/homebrew/opt/openssl@3/include \
-L/opt/homebrew/opt/openssl@3/lib \
-lssl -lcrypto -framework Security -framework CoreFoundation
g++ client.cpp -o client -std=c++11 -pthread \
-I/opt/homebrew/opt/openssl@3/include \
-L/opt/homebrew/opt/openssl@3/lib \
-lssl -lcrypto -framework Security -framework CoreFoundation
โ๏ธ ์คํ / Run
— ์๋ฒ ํด๋ผ์ด์ธํธ๋ฅผ ์๋ก ๋ค๋ฅธ ํฐ๋ฏธ๋์์ ์คํํฉ๋๋ค.
Run the server and client in different terminals.
./server # server
./client # client

