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 :
Code:
{
Q_OBJECT
public :
virtual ~Tool();
void listClients();
public slots :
void readXml( QNetworkReply* );
void slotError(QNetworkReply * reply, QAuthenticator * authenticator);
private :
QNetworkAccessManager manager;
};
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".
Code:
void Tool::readXml( QNetworkReply* reply )
{
qDebug() << "readXml()" ;
QXmlStreamReader xmlReply( reply );
while( !xmlReply.atEnd() ) {
qDebug() << "Reading next element.";
xmlReply.readNext();
if (xmlReply.isEndElement()) {
qDebug() << "Found END element for " << xmlReply.name() ;
break;
}
if (xmlReply.isStartElement()) {
qDebug() << "Found START element for " << xmlReply.name();
if (xmlReply.name() == "response")
qDebug() << "Response Status : " << xmlReply.attributes().value("status");
else if ( xmlReply.name() == "name" )
qDebug() << "Name : " << xmlReply.readElementText();
else if ( xmlReply.name() == "error" )
qDebug() << "Error : " << xmlReply.readElementText();
//This is the error condition that is hit all the time. And says that the xml is not correctly formatted.
}//isStartElement
}
if (xmlReply.hasError()) {
qDebug() << "Xml Reply has errors." << xmlReply.errorString();
qDebug() << xmlReply.error();
}
}
This is where the xml is prepared and the request to the server is made.
Code:
void Tool::listClients()
{
qDebug() << "listClients" ;
QNetworkRequest request;
request.setUrl(serviceUrl);
request.setRawHeader("User-Agent", "Tool 0.0.1");
std::stringstream strstream;
strstream << "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
<< "<request method=\"client.list\">"
<< "</request>";
std::cout << "Posted Xml : " << strstream.str();
manager.post(request, data);
}
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
Code:
QNetworkAccessManager::post()
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.
Re: How to cccess XML api correctly?
err, I meant "How to access XML api correctly?". Sorry about the typo.
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
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?
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.
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 -
Code:
<response status="fail">
<error>Incorrectly formatted xml</error>
</response>
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.