采用httplib与rapidjson来实现客户端发送key\value到服务器,服务器根据key解析value

服务器程序:

#include "httplib.h"

#include <string>

using namespace httplib;

int main()

{

    Server svr;

    svr.Get("/hi", [](const Request& req, Response& res) 

    {

        auto itrFindName = req.params.find("name");

        if (itrFindName != req.params.end())

        {

            std::string strName = itrFindName->second;

        }

        auto itrFindNote = req.params.find("note");

        res.set_content("Hello World!", "text/plain");

    });

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

    {

        svr.stop();

    });

    svr.listen("localhost", 1234);

    return 0;

}

客户端程序:

#include "httplib.h"

#include "document.h"

#include <string>

using namespace httplib;

using namespace std;

int main()

{

    httplib::Client cli("localhost", 1234);

    httplib::Params params;

    params.emplace("name", "john");

    params.emplace("note", "coder");

    Headers headers;

    if(auto res = cli.Get("/hi", params, headers))

    {

        if (res->status == 200) 

        {

            std::cout << res->body << std::endl;

        }

    }

    else 

    {

        auto err = res.error();

    }

    return 0;

}