httplib 与 json.hpp 结合示例
1、使用POST 接口,发送 登陆 请求
客户端发送
{
nlohmann::json jsonOfCollectionInfo;
jsonOfCollectionInfo["user_id"] = "zhang";
jsonOfCollectionInfo["password"] = "123456";
httplib::Client cli(collector_server_address);
auto reponse = cli.Post("/login_in", jsonOfCollectionInfo.dump(), "text/plain");
}
服务端接收
{
// HTTP
httplib::Server svr_http;
svr_http.Post("/login_in", [](const httplib::Request &request, httplib::Response &res)
{
std::cout << "[" << __FILE__ << "][" << __LINE__ << "] recevie post msg begin" << request.body << std::endl;
auto request = nlohmann::json::parse(request->body);
......
std::cout << 输出 request 的信息 << std::endl;
.....
});
}
2、使用get 接口,获取联网状态
客户端发送
httplib::Client cli("http://0.0.0.0:8188");
auto res_client = cli.Get("/get_network_status");
if(res_client == NULL)
{
return false;
}
std::cout << res_client->status << std::endl;
std::cout << res_client->body << std::endl;
auto json_result = nlohmann::json::parse(res_client->body);
服务端接收
{
// HTTP
httplib::Server svr_http;
svr_http.Post("/get_network_status", [](const httplib::Request &request, httplib::Response &res)
{
std::cout << "[" << __FILE__ << "][" << __LINE__ << "] recevie post msg begin" << request.body << std::endl;
auto request = nlohmann::json::parse(request->body);
......
std::cout << 输出 request 的信息 << std::endl;
.....
});
}