17

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

Community
  • 1
  • 1
Horst Walter
  • 13,663
  • 32
  • 126
  • 228
  • 1
    Why not create it as a CGI program being called by an existing web-server? It's probably much simpler. – Some programmer dude Jul 19 '12 at 10:09
  • Because it is basically a desktop scenario. The data reside on the user's PC only, and it is not possible to install a web server on each user's PC. The WebApp is only used because it is featuring Google Maps. – Horst Walter Jul 19 '12 at 10:31
  • 1
    Have you considered [Jetty](http://jetty.codehaus.org/jetty/)? IIRC there's no installation needed and it just needs Java. – Samuel Harmer Jul 19 '12 at 10:33
  • Not Jetty in particular, but the approach with a web server. The application itself needs to provide it, no additional installations are feasible, needs to run without Java. Thanks for the hint! – Horst Walter Jul 19 '12 at 10:39
  • This seems to be the Qt Jetty :-; http://stackoverflow.com/questions/3122508/qt-http-server – Horst Walter Jul 19 '12 at 11:07
  • 1
    UPDATE: [gSOAP](http://http://www.genivia.com/tutorials.html#How_to_use_JSON_and_JSONPath_with_gSOAP) has an updated JSON engine and a new jsoncpp code generator that makes it [really easy to write JSON code](http://www.genivia.com/doc/xml-rpc-json/html/index.html) in C++ (and C) and deploy as a service. – Dr. Alex RE Feb 03 '16 at 19:22

2 Answers2

16

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.

Community
  • 1
  • 1
Horst Walter
  • 13,663
  • 32
  • 126
  • 228
1
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.

maazza
  • 7,016
  • 15
  • 63
  • 96
Stefan
  • 1,789
  • 1
  • 11
  • 16