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

后端 未结 2 1372
一生所求
一生所求 2021-01-30 11:33

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 Ja

相关标签:
2条回答
  • 2021-01-30 12:23

    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.

    0 讨论(0)
  • 2021-01-30 12:39
    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.

    0 讨论(0)
提交回复
热议问题