Creating simple WebService in C++ / Qt (acting as server) providing JSON data Creating simple WebService in C++ / Qt (acting as server) providing JSON data json json

Creating simple WebService in C++ / Qt (acting as server) providing JSON data


As of my tests, currently I am using QtWebApp: http://stefanfrings.de/qtwebapp/index-en.html This is one of the answers of Edit 2 ( Qt HTTP Server? )

Stefan's small WebServer has some well documented code, is written in "Qt C++" and easy to use, especially if you have worked with servlets already. Since it can be easily integrated in my Qt project, I'll end up with an internal WebServer.

Some demo code from my JSON tests, showing that generating the JSON content is basically creating a QString.

void WebServiceController::service(HttpRequest& request, HttpResponse& response) {// set some headersresponse.setHeader("Content-Type", "application/json; charset=ISO-8859-1");response.setCookie(HttpCookie("wsTest","CreateDummyPerson",600));QString dp = WebServiceController::getDummyPerson();QByteArray ba = dp.toLocal8Bit();const char *baChar = ba.data();response.write(ba);}

If someone has easy examples with other libs to share, please let me know.


QByteArray ba = dp.toLocal8Bit();const char *baChar = ba.data();

You don't need to convert the QByteArray to char array. Response.write() can also be called with a QByteArray.

By the way: qPrintable(dp) is a shortcut to convert from QString to char array.