cpp http server/client

news/2024/10/5 23:02:29/
http://www.w3.org/2000/svg" style="display: none;">

httplib_0">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.ppmy.cn/news/1473675.html

相关文章

Hi3861 OpenHarmony嵌入式应用入门--wifi hotspot

鸿蒙WiFi AP模式相关的API接口文件路径 foundation/communication/interfaces/kits/wifi_lite/wifiservice/wifi_hotspot_config.h foundation/communication/interfaces/kits/wifi_lite/wifiservice/wifi_hotspot.h 所使用的API接口有&#xff1a; API 接口说明 WifiErro…

邀请函 | 极限科技全新搜索引擎 INFINI Pizza 亮相 2024 可信数据库发展大会!

过去一年&#xff0c;在全球 AI 浪潮和国家数据局成立的推动下&#xff0c;数据库产业变革不断、热闹非凡。2024 年&#xff0c;站在中国数字经济产业升级和数据要素市场化建设的时代交汇点上&#xff0c;“2024 可信数据库发展大会” 将于 2024 年 7 月 16-17 日在北京悠唐皇冠…

Windows如何查看端口是否占用,并结束端口进程

需求与问题&#xff1a;前后端配置了跨域操作&#xff0c;但是仍然报错&#xff0c;可以考虑端口被两个程序占用&#xff0c;找不到正确端口或者后端接口书写是否规范&#xff0c;特别是利用Python Flask书写时要保证缩进是否正确&#xff01; Windows操作系统中&#xff0c;查…

昇思25天学习打卡营第16天|Diffusion扩散模型

导入必要的库函数 import math from functools import partial %matplotlib inline import matplotlib.pyplot as plt from tqdm.auto import tqdm import numpy as np from multiprocessing import cpu_count from download import downloadimport mindspore as ms import mi…

贵州建筑三类人员安全员2024年考试最新题库练习题

一、单选题 1.建设工程安全管理的方针是&#xff08;&#xff09;。 A.安全第一&#xff0c;预防为主&#xff0c;综合治理 B.质量第一&#xff0c;兼顾安全 C.安全至上 D.安全责任重于泰山 答案&#xff1a;A 2.安全生产管理的根本目的是&#xff08;&#xff09;。 A.…

ubuntu的screen会话,断开远程连接也能照样运行程序

文章目录 创建新的 screen 会话管理 screen 会话例子关闭某一个 screen 会话删除某一个 screen 会话 在 Ubuntu 中使用 screen 工具可以创建和管理多个终端会话。以下是创建 screen 会话的方法&#xff1a; 创建新的 screen 会话 启动一个新的 screen 会话&#xff1a; screen…

Java实现登录验证 -- JWT令牌实现

目录 1.实现登录验证的引出原因 2.JWT令牌2.1 使用JWT令牌时2.2 令牌的组成 3. JWT令牌&#xff08;token&#xff09;生成和校验3.1 引入JWT令牌的依赖3.2 使用Jar包中提供的API来实现JWT令牌的生成和校验3.3 使用JWT令牌验证登录3.4 令牌的优缺点 1.实现登录验证的引出 传统…

huggingface笔记:gpt2

0 使用的tips GPT-2是一个具有绝对位置嵌入的模型&#xff0c;因此通常建议在输入的右侧而不是左侧填充GPT-2是通过因果语言建模&#xff08;CLM&#xff09;目标进行训练的&#xff0c;因此在预测序列中的下一个标记方面非常强大 利用这一特性&#xff0c;GPT-2可以生成语法连…