Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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? "

like image 666
Horst Walter Avatar asked Jul 19 '12 09:07

Horst Walter


People also ask

Does Qt support JSON?

Qt provides support for dealing with JSON data. JSON is a format to encode object data derived from Javascript, but now widely used as a data exchange format on the internet. The JSON support in Qt provides an easy to use C++ API to parse, modify and save JSON data.


2 Answers

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");
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.

like image 154
Horst Walter Avatar answered Oct 03 '22 02:10

Horst Walter


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.

like image 22
Stefan Avatar answered Oct 03 '22 00:10

Stefan