An easy to use http client/server based on libevent writen in c++.
mkdir -p libevent/build
cd libevent
git clone https://github.com/libevent/libevent.git src --depth 1
cd build
cmake ../src -G "Ninja" -DEVENT__DISABLE_OPENSSL=ON -DEVENT__DISABLE_MBEDTLS=ON -DEVENT__LIBRARY_TYPE=STATIC -DCMAKE_INSTALL_PREFIX=../install -DCMAKE_BUILD_TYPE=Release
cmake --build . --target install -- -j4
mkdir -p easy_http/build
cd easy_http
git clone https://github.com/ma-mehralian/easy_http.git src
cd build
cmake ../src -G "Ninja" -DLibevent_DIR=path/to/cmake/libevent -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=../install
cmake --build . --target install -- -j4
#include <iostream>
#include <easy_http/client.h>
#include <easy_http/response.h>
int main(int argn, char* argc[]) {
Client c("http://postman-echo.com/");
try {
auto req = c.Post("/post",
[](const Response& res) {
std::cout << "response[" << res.GetStatusCode() << "]: " << res.GetContent() << std::endl;
})
.PushHeader("test_header", "test_value")
.PushParam("test_query", "test_value");
std::cout << "calling: " << req.FullUrl() << std::endl;
req.Send();
}
catch (const std::exception& e) {
std::cout << "request failed: " << e.what() << std::endl;
}
return 0;
}
#include <iostream>
#include <easy_http/server.h>
using namespace std;
Response Test(Request& request) {
cout << "test called" << endl;
Response response(request, 200);
response.SetContent("Test callback");
return response;
}
int main(int argn, char* argc[]) {
string ip = "0.0.0.0";
int port = 4000;
Server s(ip, port);
s.Get("/test", Test);
if (s.Start() != 0){
cout << "Cannot start HTTP server http://" << ip << ":" << port << endl;
return -1;
}
return 0;
}