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.