mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2025-05-15 01:08:27 +00:00
Add example/server_and_client.cc
This commit is contained in:
parent
8415bf0823
commit
01a52aa8bd
3 changed files with 51 additions and 1 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -9,6 +9,8 @@ example/benchmark
|
||||||
example/redirect
|
example/redirect
|
||||||
example/sse*
|
example/sse*
|
||||||
example/upload
|
example/upload
|
||||||
|
example/one_time_request
|
||||||
|
example/server_and_client
|
||||||
example/*.pem
|
example/*.pem
|
||||||
test/httplib.cc
|
test/httplib.cc
|
||||||
test/httplib.h
|
test/httplib.h
|
||||||
|
|
|
@ -53,9 +53,12 @@ benchmark : benchmark.cc ../httplib.h Makefile
|
||||||
one_time_request : one_time_request.cc ../httplib.h Makefile
|
one_time_request : one_time_request.cc ../httplib.h Makefile
|
||||||
$(CXX) -o one_time_request $(CXXFLAGS) one_time_request.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) $(BROTLI_SUPPORT)
|
$(CXX) -o one_time_request $(CXXFLAGS) one_time_request.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) $(BROTLI_SUPPORT)
|
||||||
|
|
||||||
|
server_and_client : server_and_client.cc ../httplib.h Makefile
|
||||||
|
$(CXX) -o server_and_client $(CXXFLAGS) server_and_client.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) $(BROTLI_SUPPORT)
|
||||||
|
|
||||||
pem:
|
pem:
|
||||||
openssl genrsa 2048 > key.pem
|
openssl genrsa 2048 > key.pem
|
||||||
openssl req -new -key key.pem | openssl x509 -days 3650 -req -signkey key.pem > cert.pem
|
openssl req -new -key key.pem | openssl x509 -days 3650 -req -signkey key.pem > cert.pem
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm server client hello simplecli simplesvr upload redirect ssesvr ssecli benchmark one_time_request *.pem
|
rm server client hello simplecli simplesvr upload redirect ssesvr ssecli benchmark one_time_request server_and_client *.pem
|
||||||
|
|
45
example/server_and_client.cc
Normal file
45
example/server_and_client.cc
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
//
|
||||||
|
// server_and_client.cc
|
||||||
|
//
|
||||||
|
// Copyright (c) 2024 Yuji Hirose. All rights reserved.
|
||||||
|
// MIT License
|
||||||
|
//
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
#include <string>
|
||||||
|
#include <httplib.h>
|
||||||
|
|
||||||
|
using namespace httplib;
|
||||||
|
|
||||||
|
const char *HOST = "localhost";
|
||||||
|
const int PORT = 1234;
|
||||||
|
|
||||||
|
const std::string JSON_DATA = R"({"hello": "world"})";
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
Server svr;
|
||||||
|
|
||||||
|
svr.Post("/api", [&](const Request & /*req*/, Response &res) {
|
||||||
|
res.set_content("Hello World!", "text/plain");
|
||||||
|
});
|
||||||
|
|
||||||
|
auto thread = std::thread([&]() { svr.listen(HOST, PORT); });
|
||||||
|
|
||||||
|
auto se = detail::scope_exit([&] {
|
||||||
|
svr.stop();
|
||||||
|
thread.join();
|
||||||
|
});
|
||||||
|
|
||||||
|
svr.wait_until_ready();
|
||||||
|
|
||||||
|
Client cli(HOST, PORT);
|
||||||
|
|
||||||
|
auto res =
|
||||||
|
cli.Post("/api", Headers(), JSON_DATA.data(), JSON_DATA.size(),
|
||||||
|
"application/json", [](uint64_t, uint64_t) { return true; });
|
||||||
|
|
||||||
|
if (res) {
|
||||||
|
std::cout << res->body << std::endl;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue