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 小时前
博物馆展览门户|基于Java博物馆展览门户系统(源码+数据库+文档)
java·开发语言·数据库·vue.js·spring boot·后端
liulilittle3 小时前
FileStream C++
开发语言·c++·cocoa
Gomiko3 小时前
C/C++基础(五):分支
c语言·c++
点PY3 小时前
C++ 中 std::async 和 std::future 的并发性
java·开发语言·c++
不会代码的小猴3 小时前
C++的第九天笔记
开发语言·c++·笔记
CoderYanger4 小时前
Java SE——12.异常(≠错误)《干货笔记》
java·开发语言
Data_agent4 小时前
1688获得1688店铺所有商品API,python请求示例
java·开发语言·python
一晌小贪欢4 小时前
【Python办公】-图片批量添加文字水印(附代码)
开发语言·python·图片水印·python水印·python添加水印·图片添加水印
Yeats_Liao4 小时前
CANN Samples(十三):Ascend C 算子开发入门
c语言·开发语言
越来越无动于衷4 小时前
Java 实现 WebService(SOAP)联网调用:从原理到实战
java·开发语言