c++ http第一个服务

c++ http第一个服务

一、下载相关依赖:这是一个git开源项目 代码仓地址

二、演示代码,编译参数:g++ test.cpp -I/**** -lpthread

c++ 复制代码
#include <httplib.h>
using namespace httplib;

void wuhan(const Request &req, Response &res)
{
    printf("httplib server recv a req: %s\n ", req.path.c_str() );
    res.set_content("<html>  \
                        <meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\"> \
                        <h1> 武汉, 加油!</h1></html>",
                    "text/html");
    res.status = 200;
}

int main(void)
{
    using namespace httplib;

    Server svr;
    svr.set_base_dir("./");

/// Get
svr.Get("/wuhan", wuhan);


    svr.Get("/hi", [](const Request& req, Response& res) {
        res.set_content("Hello World!", "text/plain");
    });

    svr.Get(R"(/numbers/(\d+))", [&](const Request& req, Response& res) {
        auto numbers = req.matches[1];
        res.set_content(numbers, "text/plain");
    });

    svr.Get("/body-header-param", [](const Request& req, Response& res) {
        if (req.has_header("Content-Length")) {
            auto val = req.get_header_value("Content-Length");
        }
        if (req.has_param("key")) {
            auto val = req.get_param_value("key");
        }
        res.set_content(req.body, "text/plain");
    });

    svr.Get("/stop", [&](const Request& req, Response& res) {
        svr.stop();
    });

    /// listen
    svr.listen("localhost", 1234);
}

三、访问网址http://localhost:1234/hi

相关推荐
龙侠九重天3 分钟前
C# 构建 AI Agent 系统 — 我的实践笔记
开发语言·人工智能·语言模型·自然语言处理·大模型·agent·智能体
SilentSamsara3 分钟前
Pandas 工程化:多层索引、分组聚合与窗口函数的进阶用法
开发语言·python·青少年编程·pandas
何以解忧,唯有..5 分钟前
Python 字符串完全指南:从基础到高级操作
开发语言·python
kiss strong7 分钟前
自制请求工具
开发语言·python·lua
scan7248 分钟前
短期记忆记忆存储在内存里,一个会话里的多轮对话
开发语言·c#
Highcharts.js11 分钟前
通过CSS变量实现图表色彩与逻辑解耦、图表主题统一|Highcharts Palette 详解
c++·echarts·highcharts·可视化开发·palette·styledmode·图表样式
玖玥拾16 分钟前
C/C++ 基础笔记(八)
c语言·c++
郝学胜_神的一滴27 分钟前
Qt 高级开发 027: QTabWidget自定义样式表美化实战
c++·qt
程序员皮皮林35 分钟前
Dubbo 的 SPI 和 JDK 的 SPI 有什么区别?
java·开发语言·dubbo
啦啦啦啦啦zzzz35 分钟前
数据结构:哈夫曼编码
数据结构·c++·哈夫曼编码