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

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-10 09:14:07       99 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-10 09:14:07       107 阅读
  3. 在Django里面运行非项目文件

    2024-07-10 09:14:07       90 阅读
  4. Python语言-面向对象

    2024-07-10 09:14:07       98 阅读

热门阅读

  1. Html利用Vue动态加载单文件页面【httpVueLoader】

    2024-07-10 09:14:07       30 阅读
  2. linux:命令执行过程【图表】

    2024-07-10 09:14:07       26 阅读
  3. 系统架构设计师——网络设计

    2024-07-10 09:14:07       33 阅读
  4. SSL证书到期自动巡检脚本-推送钉钉告警

    2024-07-10 09:14:07       30 阅读
  5. 如何才能在Linux下编写驱动程序

    2024-07-10 09:14:07       28 阅读
  6. Tomcat打破双亲委派模型的方式

    2024-07-10 09:14:07       32 阅读
  7. C++惯用法: 通过std::decltype来SFINAE掉表达式

    2024-07-10 09:14:07       23 阅读
  8. HTTP 范围Range请求

    2024-07-10 09:14:07       28 阅读
  9. React 开发报错整理

    2024-07-10 09:14:07       36 阅读