如何在http_request内发送 JSON 对象以及如何在服务器上访问它

How to send JSON object inside the http_request and how to access it on the server?

本文关键字:服务器 访问 对象 http request JSON      更新时间:2023-10-16

我用C++ REST SDK(卡萨布兰卡)创建了一个侦听器。我需要创建一个 http 服务器,它应该读取嵌入在请求消息正文中的 JSON 对象。如何实现这一点?我试图通过 Put 方法做到这一点。但是我找不到提取请求消息正文的方法。如果我可以在 put 方法中将该 json 对象转换为字符串以便打印它,那就更好了。但我找不到办法做到这一点。有没有可能的方法可以访问该 json 对象并将其转换为字符串?

这是我正在尝试的方法。

void WsListener::handle_put(http_request message)
{
    cout << message.body.extract_json();
};
void WsListener::handle_put(http_request message)
{
    pplx::task<utility::string_t> body_json = message.extract_string();
            std::string jsonstr=    utility::conversions::to_utf8string(body_json.get());
            cout << jsonstr << endl;
};

http://microsoft.github.io/cpprestsdk/classweb_1_1http_1_1http__request.html#a0cd0f1c49cab39210517925a3644db1f