如何使用JSON实现带有POCO C++网络库的REST API服务器?

Gut*_*Mac 5 c++ rest json poco-libraries

我一直在研究如何做到这一点,所有的例子都是text/html.我已经尝试使用带有POCO C++网络库的JSON实现服务器api休息,但我不确定这是否是正确的方法.

void MyHandler::handleRequest(HTTPServerRequest& request,      HTTPServerResponse& response)
{
   response.setStatus(HTTPResponse::HTTP_OK);
   response.setContentType("application/json");

   std::ostream& ostr = response.send();
   string send("true");
   ostr << send;
   response.setContentLength(send.size());
}
Run Code Online (Sandbox Code Playgroud)

最初它是为hmtl连接实现的:

void MyHandler::handleRequest(HTTPServerRequest& request,      HTTPServerResponse& response)
{
   response.setStatus(HTTPResponse::HTTP_OK);
   response.setContentType("text/html");

   std::ostream& ostr = response.send();
   ostr << "<html><head><title>HTTPTimeServer powered by POCO C++ Libraries</title>";
   ostr << "<body><p style=\"text-align: center; font-size: 48px;\">";
   ostr << "ConfigHandler";
   ostr << "</p></body></html>";
}
Run Code Online (Sandbox Code Playgroud)

我是否正确地完成了更改或者我错过了什么?

如果有人知道如何使用带有POCO C++库的JSON构建API REST的教程,我们将非常感激.

提前致谢.

eds*_*res 2

Poco C++ 库是在现代 C++ 中构建REST API的绝佳工具,尽管在架构方面需要做出一些设计决策。

我在GitHub上提供了一个使用Poco用 C++ 构建的API项目的示例