Results 1 to 3 of 3

Thread: Building RESTful webservice using qxt

  1. #1
    Join Date
    Aug 2011
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Building RESTful webservice using qxt

    Hi there,

    I want to build a RESTful webservice. I could use some advice on my draft.
    I found an example on how to build a soap-webservice here and used the structure of it.

    Here's the interface of an resource:
    Qt Code:
    1. #include <QString>
    2.  
    3. #include <QxtWebServiceDirectory>
    4. #include <QxtWebRequestEvent>
    5.  
    6. class IResource : public QxtWebServiceDirectory
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. IResource(QString name, QxtAbstractWebSessionManager* sm, QObject* parent = 0):
    12. QxtWebServiceDirectory(sm, parent),
    13. name(name)
    14. {
    15. }
    16.  
    17. virtual ~IResource()
    18. {
    19. }
    20.  
    21. protected:
    22. void indexRequested(QxtWebRequestEvent *event)
    23. {
    24. if (event->method.compare("GET") == 0)
    25. {
    26. doGET(event);
    27. }
    28. else if (event->method.compare("PUT") == 0)
    29. {
    30. doPUT(event);
    31. }
    32. else if (event->method.compare("POST") == 0)
    33. {
    34. doPOST(event);
    35. }
    36. else if (event->method.compare("DELETE") == 0)
    37. {
    38. doDELETE(event);
    39. }
    40. else
    41. {
    42. // not implemented - status code: 501
    43. }
    44. }
    45.  
    46. private:
    47. void doGET(QxtWebRequestEvent *event)
    48. {
    49. // for now just say your name
    50. postEvent(new QxtWebPageEvent(event->sessionID, event->requestID, name.toAscii()));
    51. }
    52.  
    53. void doPUT(QxtWebRequestEvent *event)
    54. {
    55. }
    56.  
    57. void doPOST(QxtWebRequestEvent *event)
    58. {
    59. }
    60.  
    61. void doDELETE(QxtWebRequestEvent *event)
    62. {
    63. }
    64.  
    65. QString name;
    66. };
    To copy to clipboard, switch view to plain text mode 
    Each resource should implement the Http-methods (to do something useful)
    A session manager or as I called it a "dispatcher" handles the requests (sorry about the naming confusion):
    Qt Code:
    1. #include <QxtHttpSessionManager>
    2.  
    3. #include "IResource.h"
    4.  
    5. class WebserviceDispatcher : public QxtHttpSessionManager
    6. {
    7. Q_OBJECT
    8.  
    9. public:
    10. WebserviceDispatcher(const QHostAddress &host , quint16 port)
    11. {
    12. QxtHttpSessionManager(this);
    13. rootService = new IResource("root", this, this);
    14. setPort(port);
    15. setListenInterface(host);
    16. setConnector(HttpServer);
    17. setStaticContentService(rootService);
    18.  
    19. qDebug() << "WebServer Started!";
    20. }
    21.  
    22. virtual ~WebserviceDispatcher()
    23. {
    24. delete this->rootService;
    25. }
    26.  
    27. void addService(QString uri, IResource* newService)
    28. {
    29. rootService->addService(uri, newService);
    30. }
    31.  
    32. protected:
    33. void incomingRequest(quint32 requestID, const QHttpRequestHeader & header, QxtWebContent *deviceContent)
    34. {
    35. QxtHttpSessionManager::incomingRequest(requestID, header, deviceContent);
    36. qDebug() << "Request: "<< requestID;
    37. qDebug() << "Method: " << header.method();
    38. qDebug() << "URI: " << header.path();
    39. }
    40.  
    41. private:
    42. IResource* rootService;
    43. };
    To copy to clipboard, switch view to plain text mode 

    finally main looks like this:

    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2.  
    3. #include "WebserviceDispatcher.h"
    4. #include "IResource.h"
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8. QCoreApplication a(argc, argv);
    9.  
    10. WebserviceDispatcher* myDispatcher = new WebserviceDispatcher(QHostAddress("localhost"), 8080);
    11. IResource* service1 = new IResource("service1", myDispatcher, 0);
    12. IResource* service2 = new IResource("service2", myDispatcher, 0);
    13. IResource* subService1 = new IResource("subService1", myDispatcher, service1);
    14. IResource* subService2 = new IResource("subService2", myDispatcher, service2);
    15.  
    16. service1->addService("sub1", subService1);
    17. myDispatcher->addService("service1", service1);
    18.  
    19. service2->addService("sub2", subService2);
    20. myDispatcher->addService("service2", service2);
    21.  
    22. myDispatcher->start();
    23.  
    24. return a.exec();
    25. }
    To copy to clipboard, switch view to plain text mode 

    this example is working and lets you navigate to the uris:


    The service shall provide and accept json objects.
    I am not an expert on the topic so any opinion is appreciated (more precisely: is that the intended use of the qxt-classes? Has anyone done that better already and is willing to share ideas?)

    Thanks
    Last edited by MichaII; 9th August 2011 at 10:34.

  2. #2
    Join Date
    Sep 2013
    Posts
    1
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Windows Android

    Default Re: Building RESTful webservice using qxt

    hi,
    why do I not run it well in my computer.I create the project with vs2008 + QT.Now I can't response to the browser.

  3. #3
    Join Date
    Mar 2013
    Posts
    24
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Building RESTful webservice using qxt

    Ok, well I tried and everything seems ok.

    I've a question: how can I use last string in the path as parameters?
    For example I'd like to do: http://localhost:8080/service1/sub1/1 where 1 is a parameter to pass to the sub1 not another service to register.

    Thanks

Similar Threads

  1. Webservice with QT
    By BP in forum Newbie
    Replies: 9
    Last Post: 16th June 2011, 05:08
  2. Using webservice in Qt
    By mind_freak in forum Qt Programming
    Replies: 1
    Last Post: 16th May 2011, 06:50
  3. Building Qt 4.6 rc
    By eekhoorn12 in forum Installation and Deployment
    Replies: 4
    Last Post: 3rd December 2009, 07:13
  4. qtsoap with .net webservice
    By drbergie in forum Qt Programming
    Replies: 11
    Last Post: 12th February 2009, 08:46
  5. Calling a webservice
    By steg90 in forum Qt Programming
    Replies: 8
    Last Post: 17th December 2007, 18:29

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.