当前位置: 首页 > news >正文

cpp http server/client

httplib

使用httplib库

basedemo

server.cpp

#include "httplib.h"
#include <iostream>
using namespace httplib;int main(void)
{Server svr;svr.Get("/hello", [](const Request& req, Response& res) {std::cout << "log, path=" << req.path << std::endl;auto it = req.params.find("name");std::string name;if(it != req.params.end()){name = it->second;}res.set_content("Hello "+name, "text/plain");});svr.listen("0.0.0.0", 50000);
}

curl一下

$ curl "127.0.0.1:50000/hello?name=zhangsan"
Hello zhangsan%

client.cpp

#include <httplib.h>
#include <iostream>using namespace httplib;int main(void){Client cli("127.0.0.1", 50000);if (auto res = cli.Get("/hello?name=zhangsi")) {std::cout << res->status << std::endl;std::cout << res->get_header_value("Content-Type") << std::endl;std::cout << res->body << std::endl;} else {std::cout << "error code: " << res.error() << std::endl;}return 0;
}

run client

$ g++ tclient.cpp -g -std=c++11 -o tclient && ./tclient
200
text/plain
Hello zhangsi

commondemo

其中struct和json转换可以参见cpp struct json相互转换

server.cpp

#include <unistd.h>
#include "httplib.h"
#include <iostream>
#include <mockutil.h>
using namespace httplib;int main(void)
{Server svr;UserUtil mock_user_util;svr.Get("/hello", [](const Request& req, Response& res) {std::cout << "log, path=" << req.path << std::endl;auto it = req.params.find("name");std::string name;if(it != req.params.end()){name = it->second;}res.set_content("Hello "+name, "text/plain");});svr.Get(R"(/user/(\w+)/get)", [&](const Request& req, Response& res) {auto uname = req.matches[1];User* user = mock_user_util.get(uname.str());std::string ret;if(user==nullptr){ret = "get no user(name="+uname.str()+")";}else{json j;user->to_json(j);ret = j.dump();}res.set_content(ret, "text/plain");});svr.Post(R"(/user/(\w+)/set)", [&](const Request& req, Response& res) {auto uname = req.matches[1];User user;user.from_json(req.body);mock_user_util.set(user);res.set_content("ok", "text/plain");});svr.listen("0.0.0.0", 50001);std::cout << "1111" << std::endl;}

client.cpp

#include <httplib.h>
#include <iostream>
#include "mockutil.h"
using namespace httplib;int main(void){Client cli("127.0.0.1", 50001);auto res = cli.Get("/user/zhangwu/get");User user;user.from_json(res->body);std::cout << "get user, name=" << user.Name << ", Phone=" << user.MPhone.Num << std::endl;user.MPhone.Num = 88888;json j;user.to_json(j);auto res2 = cli.Post("/user/zhangwu/set", j.dump(), "application/json");std::cout << "set user, name=" << user.Name << ", Phone=" << user.MPhone.Num << std::endl;auto res3 = cli.Get("/user/zhangwu/get");User userx;userx.from_json(res3->body);std::cout << "get after set, name=" << userx.Name << ", Phone=" << userx.MPhone.Num << std::endl;return 0;
}

run client

$ g++ tclient.cpp -g -std=c++11 -I./ -o tclient && ./tclient
get user, name=zhangwu, Phone=12345
set user, name=zhangwu, Phone=88888
get after set, name=zhangwu, Phone=88888
http://www.lryc.cn/news/393202.html

相关文章:

  • 昇思25天学习打卡营第2天|MindSpore快速入门
  • django之url路径
  • 【OnlyOffice】桌面应用编辑器,插件开发大赛,等你来挑战
  • [学习笔记]SQL学习笔记(连载中。。。)
  • Buuctf之SimpleRev做法
  • 【云原生监控】Prometheus 普罗米修斯从搭建到使用详解
  • 【C++】模板进阶--保姆级解析(什么是非类型模板参数?什么是模板的特化?模板的特化如何应用?)
  • Cookie与Session
  • Nuxt3 的生命周期和钩子函数(十一)
  • Windows ipconfig命令详解,Windows查看IP地址信息
  • 在C#/Net中使用Mqtt
  • VBA提取word表格内容到excel
  • html+css+js图片手动轮播
  • 【十三】图解 Spring 核心数据结构:BeanDefinition 其二
  • 数据库作业
  • 12、matlab中for循环,if else判断语句,break和continue用法以及switch case语句使用
  • AcWing 3207:门禁系统 ← 桶排序中“桶”的思想
  • 开发个人Go-ChatGPT--3 服务拆分
  • Android --- 新电脑安装Android Studio 使用 Android 内置模拟器电脑直接卡死,鼠标和键盘都操作不了
  • 从入门到深入,Docker新手学习教程
  • Postman编写测试脚本
  • 代码随想录算法训练Day57|LeetCode200-岛屿数量、LeetCode695-岛屿的最大面积
  • StopWatch的使用
  • MySQL基础篇(三)数据库的修改 删除 备份恢复 查看连接情况
  • android手机电视相框项目-学员做出个bug版本邀请大家review提意见
  • web零碎知识2
  • Android项目框架
  • vue 模糊查询加个禁止属性
  • MySQL 主从复制中 MHA 工具的研究与实践
  • Hi3861 OpenHarmony嵌入式应用入门--TCP Server