5

I have been researching about how to do this and all the examples are with text/html. I have tried implement a server api rest using JSON with POCO C++ network libraries but I am not quite sure if it is the correct way to do this.

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());
}

Originally it was implemented for hmtl connections as:

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>";
}

Have I done the change correctly or am I missing something??

If anyone knows of a tutorial about how to build an API REST using JSON with POCO C++ Libraries, it will be very much appreciated.

Thanks in advance.

ne1410s
  • 6,864
  • 6
  • 55
  • 61
GutiMac
  • 2,402
  • 1
  • 20
  • 27
  • 1
    Is there any problem with your code? Doesn't it work as you expect it to? You *have* tried it? – Some programmer dude Feb 12 '16 at 12:25
  • Yes, I have tried it and it works, but I am not sure if it is the correct way. I just was asking if this correct or exists other better way because the examples in the POCO repository just use "text/xml". – GutiMac Feb 12 '16 at 13:59
  • 2
    The only difference between replying with an XML document and an JSON document is the content-type and the actual data. The rest should be all the same. – Some programmer dude Feb 12 '16 at 14:28
  • Great, Thank you. I was looking for this. – GutiMac Feb 12 '16 at 14:35

1 Answers1

2

Poco C++ Libraries is a great tool for REST APIs building in modern C++, although, regarding the architecture, there are some design decisions to be made.

I made available on GitHub an example of an API project built in C++ using Poco.

edson.a.soares
  • 1,010
  • 12
  • 12