Results 1 to 7 of 7

Thread: How to cccess XML api correctly?

  1. #1
    Join Date
    Apr 2009
    Posts
    5
    Qt products
    Qt3 Qt4 Qt/Embedded Qt Jambi PyQt3 PyQt4
    Platforms
    MacOS X Unix/X11 Windows

    Question How to cccess XML api correctly?

    Hi,

    I am new to this forum. So a big hello to all.

    I am trying to talk to a server using its Xml api. I am using QXmlStreamReader for my xml operations and QNetworkAccessManager and family of classes for network related stuff.

    The problem, I am facing is that the xml request fails every time. The response I get from server says that my xml is incorrectly formatted.

    Following is the test class :
    Qt Code:
    1. class Tool : public QObject
    2. {
    3. Q_OBJECT
    4.  
    5. public :
    6. Tool( QObject *parent = 0 );
    7. virtual ~Tool();
    8.  
    9. void listClients();
    10.  
    11. public slots :
    12. void readXml( QNetworkReply* );
    13. void slotError(QNetworkReply * reply, QAuthenticator * authenticator);
    14.  
    15. private :
    16. QNetworkAccessManager manager;
    17.  
    18. };
    To copy to clipboard, switch view to plain text mode 

    This method is simply reading the response and parsing it using QXmlStreamReader. Please note the comment near the if condition that checks for tag called "error".
    Qt Code:
    1. void Tool::readXml( QNetworkReply* reply )
    2. {
    3. qDebug() << "readXml()" ;
    4. QXmlStreamReader xmlReply( reply );
    5.  
    6. while( !xmlReply.atEnd() ) {
    7. qDebug() << "Reading next element.";
    8. xmlReply.readNext();
    9. if (xmlReply.isEndElement()) {
    10. qDebug() << "Found END element for " << xmlReply.name() ;
    11. break;
    12. }
    13. if (xmlReply.isStartElement()) {
    14. qDebug() << "Found START element for " << xmlReply.name();
    15. if (xmlReply.name() == "response")
    16. qDebug() << "Response Status : " << xmlReply.attributes().value("status");
    17. else if ( xmlReply.name() == "name" )
    18. qDebug() << "Name : " << xmlReply.readElementText();
    19. else if ( xmlReply.name() == "error" )
    20. qDebug() << "Error : " << xmlReply.readElementText();
    21. //This is the error condition that is hit all the time. And says that the xml is not correctly formatted.
    22. }//isStartElement
    23. }
    24.  
    25. if (xmlReply.hasError()) {
    26. qDebug() << "Xml Reply has errors." << xmlReply.errorString();
    27. qDebug() << xmlReply.error();
    28. }
    29.  
    30. }
    To copy to clipboard, switch view to plain text mode 

    This is where the xml is prepared and the request to the server is made.
    Qt Code:
    1. void Tool::listClients()
    2. {
    3. qDebug() << "listClients" ;
    4. QNetworkRequest request;
    5. request.setUrl(serviceUrl);
    6. request.setRawHeader("User-Agent", "Tool 0.0.1");
    7.  
    8. std::stringstream strstream;
    9. strstream << "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
    10. << "<request method=\"client.list\">"
    11. << "</request>";
    12.  
    13. std::cout << "Posted Xml : " << strstream.str();
    14.  
    15. QByteArray data(strstream.str().c_str());
    16.  
    17. manager.post(request, data);
    18. }
    To copy to clipboard, switch view to plain text mode 

    I have tried to use QByteArray directly to prepare the xml and have got the same results.

    So, what I want to know is, what am I doing wrong? Also, I am wondering if the
    Qt Code:
    1. QNetworkAccessManager::post()
    To copy to clipboard, switch view to plain text mode 
    method is sending solely XML in the request body or is it sending in something else as well? If it is sending more than the prepared XML as request, then what is the right way to send only the XML.

    Thanks.

  2. #2
    Join Date
    Apr 2009
    Posts
    5
    Qt products
    Qt3 Qt4 Qt/Embedded Qt Jambi PyQt3 PyQt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to cccess XML api correctly?

    err, I meant "How to access XML api correctly?". Sorry about the typo.

  3. #3
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to cccess XML api correctly?

    Use a network sniffer like ethereal to find out what data is sent over network, really.
    (It helps to be sure that you actually send and receive correctly formatted xml and the reply matches your expectations.)

    HTH

  4. #4
    Join Date
    Apr 2009
    Posts
    5
    Qt products
    Qt3 Qt4 Qt/Embedded Qt Jambi PyQt3 PyQt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to cccess XML api correctly?

    Thanks for the pointer. I just tried doing that wireshark and fail miserable primarily because I donot know what I should be looking for. Wireshark spitted out loads of information. What do I look for and where? Any pointers?

    I am still wondering if my way of passing xml over the network is wrong? Should I be doing it some other way?

  5. #5
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to cccess XML api correctly?

    try maybe to print the reply data with qDebug() or to some QTextBrowser or something to see how it looks like.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  6. #6
    Join Date
    Apr 2009
    Posts
    5
    Qt products
    Qt3 Qt4 Qt/Embedded Qt Jambi PyQt3 PyQt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to cccess XML api correctly?

    That is exactly what readXml() does. It takes the QNetworkReply and creates a QXmlStreamReader out of it and then parses it. The xml looks like this -
    Qt Code:
    1. <response status="fail">
    2. <error>Incorrectly formatted xml</error>
    3. </response>
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Apr 2009
    Posts
    5
    Qt products
    Qt3 Qt4 Qt/Embedded Qt Jambi PyQt3 PyQt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to cccess XML api correctly?

    For the sake of completeness, if I post the same xml using curl commandline, it works great and gives me the correct response as well.

Similar Threads

  1. Howto benchmark QT correctly?
    By Linuxhippy in forum Qt Programming
    Replies: 1
    Last Post: 8th October 2008, 08:01
  2. renderText is not working correctly
    By validator in forum Qt Programming
    Replies: 3
    Last Post: 27th May 2008, 16:55
  3. correctly installed Qt4
    By Salazaar in forum Installation and Deployment
    Replies: 31
    Last Post: 7th May 2007, 07:24
  4. Replies: 1
    Last Post: 23rd August 2006, 18:02
  5. Replies: 6
    Last Post: 20th April 2006, 10:23

Tags for this Thread

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.