Horst Walter Horst Walter - 1 month ago 57
C++ Question

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

I need to create a simple web service (being the "server"). The goal is to provide some data I do read in an Qt / C++ application as JSON data. Basically a JavaScript application in the browser shall read its data from the Qt app. It is usually a single user scenario, so the user runs a Google Maps application in her browser, while additional data come from the Qt application.

So far I have found these libs:

  1. Qxt: http://libqxt.bitbucket.org/doc/0.6/index.html but being a newbie on C++/Qt I miss some examples. Added: I have found one example here

  2. gSoap: http://www.cs.fsu.edu/~engelen/soap.html has more examples and documentation and also seems to support JSON

  3. KD SOAP: http://www.kdab.com/kdab-products/kd-soap/ with no example as far as I can tell, docu is here

  4. Qt features itself, but it is more about acting as a client: http://qt-project.org/videos/watch/qt-networking-web-services

Checking SO gives me basically links to the above libs

  1. webservice with Qt with an example I do not really get.

  2. How to Create a webservice by QT

So basically I do have the following questions:

  1. Which lib would you use? I want to keep it as simple as possible and would need an example.

  2. Is there another (easy!) way to provide the JSON data to the JavaScript Web page besides the WebService?

-- Edit, remarks: ---

Needs to be application intrinsic. No web server can be installed, no extra run time can be used. The user just runs the app. Maybe the Qt WebKit could be an approach....

-- Edit 2 --

Currently checking the tiny web servers as of SO " Qt HTTP Server? "


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 headers
response.setHeader("Content-Type", "application/json; charset=ISO-8859-1");

QString dp = WebServiceController::getDummyPerson();
QByteArray ba = dp.toLocal8Bit();
const char *baChar = ba.data();

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