libhv

1、概述

最近在做c++的客户端,调研了一圈选了libhv

libhv是c++编写HTTP API 服务端/客户端最简单的库,没有之一

具有如下特性:

  • 跨平台(Windows, Linux, Mac)
  • 支持https
  • 支持RESTful API
  • 支持application/json、application/x-www-form-urlencoded、multipart/form-data
  • 内置web service文件服务和indexof service目录服务
  • 可扩展多进程/多线程模型

libhv是一个跨平台的类似libevent、libev、libuv的异步IO事件循环库,但提供了更加简单的API接口和更加丰富的协议(包括http、ftp、smtp、dns、icmp等)。
libhv已广泛实用在公司的IOT平台、http API服务之中,正确性、稳定性、可扩展性、性能都有保证,完全开源,请放心使用。

项目地址
码云镜像

2、安装与使用

git clone https://github.com/ithewei/libhv.git
cd libhv/
sudo make
sudo make install  # 这和CMakeList.txt里面的内容有关

第一个工程:一个最简单的restful http服务器

cmake_minimum_required(VERSION 3.16)
project(libhv_test)

set(CMAKE_CXX_STANDARD 11)


#添加头文件搜索路径
include_directories(/usr/local/include/hv)
#添加库文件搜索路径
link_directories(/usr/local/lib)

#用于将当前目录下的所有源文件的名字保存在变量 DIR_SRCS 中
add_executable(libhv_test main.cpp)

#在这里根据名字boost_thread去寻找libboost_thread.a文件
target_link_libraries(libhv_test -lhv)

程序

#include "HttpServer.h"
int http_api_echo(HttpRequest* req, HttpResponse* res) {
    res->body = req->body;
    return 0;
}
int main() {
    HttpService service;
    service.base_url = "/v1/api";
    service.AddApi("/echo", HTTP_POST, http_api_echo);

    http_server_t server;
    server.port = 8080;
    server.service = &service;
    http_server_run(&server);


    return 0;
}

curl测试

# curl -H "Content-Type:application/json" -XPOST  http://localhost:8080/v1/api/echo  -d '{ "id":"3", "provinceId":"3", "cityName":"zhuhai", "description":"live in zhuhai"}'
{ "id":"3", "provinceId":"3", "cityName":"zhuhai", "description":"live in

编写客户端

发送post请求

#include "http_client.h"

int main() {
    HttpRequest req;
    req.method = HTTP_POST;
    req.url = "http://localhost:8080/v1/api/echo";
    req.body = "hello world";
    
    HttpResponse res;
    int ret = http_client_send(&req, &res);
    if (ret != 0){
        printf("* Failed:%s:%d\n", http_client_strerror(ret), ret);
    }else{
        printf("%s\n", res.Dump(true, true).c_str());
    }
    
    
    return 0;
}

cmakeList.txt文件与上面一样

3、json.hpp

另外就是这个库本身对json做了封装,十分好用

在头部加上命名空间即可使用

using namespace nlohmann;
// 创建一个空结构(null)
json j;

// 添加一个数值,存为 double
j["pi"] = 3.141;
// 添加一个布尔值,存为 bool 
j["happy"] = true;
// 添加一个字符串,存为 std::string
j["name"] = "Niels";
// 添加另一个空对象,使用 nullptr
j["nothing"] = nullptr;
// 添加一个对象内的对象
j["answer"]["everything"] = 42;
// 添加一个数组,存为 std::vector
j["list"] = {1, 0, 2};
// 添加另一个对象
j["object"] = {{"currency", "USD"}, {"value", 42.99}};

如果要创建数组

j["list"][0] = 1;
j["list"][1] = 2;

4、json反序列化与反序列化

1、字符串解析为json

    json j = "{\"happy\": true, \"pi\": 3.141}"_json;             // 从字符串字面量创建对象
    auto j2 = R"({"happy": true, "pi": 3.141})"_json;             // 最好是使用原始字符串字面量
    auto j3 = json::parse("{\"happy\": true, \"pi\": 3.141}");  // 显式地分析

    std::cout << j["happy"] <<" " <<j2["happy"] <<" " <<j3["happy"] << "\n";

2、json解析为字符串

    json j = "{\"happy\": true, \"pi\": 3.141}"_json;

    // 显式地转换至字符串
    std::string s = j.dump();

    std::cout << s << std::endl; // {"happy":true,"pi":3.141}

// 传入缩进空格数,使用良好的打印形式进行序列化
    std::cout << j.dump(4) << std::endl;
/*
 {
    "happy": true,
    "pi": 3.141
}
 */

3、反序列化为流

// 反序列化自标准输入
json j;
std::cin >> j;

// 序列化至标准输出
std::cout << j;

// 设置良好打印形式所需的缩进
std::cout << std::setw(4) << j << std::endl;

4、反序列化为文件

// 读入一个 JSON 文件
std::ifstream i("file.json");
json j;
i >> j;

// 将美化的 JSON 写入另一个文件
std::ofstream o("pretty.json");
o << std::setw(4) << j << std:endl;

更多使用参考文档:

(94条消息) C/C++编程:从0到1学习libhv(linux、clion)_OceanStar的学习笔记的博客-CSDN博客_libhv restful

(94条消息) libhv_ithewei的博客-CSDN博客

posted @ 2022-08-18 13:26  hiccup_lh  阅读(1154)  评论(0编辑  收藏  举报