Page 1 of 2 12 LastLast
Results 1 to 20 of 29

Thread: Reqest and Response in XML

  1. #1
    Join Date
    Feb 2009
    Posts
    143
    Thanks
    8

    Default Reqest and Response in XML

    Hi

    I am working on a project that uses Basic authentication scheme to authenticate a user.

    I need to call a api in a website.I need to send the username and password in base64 format.

    After the header has the host and authentication details, I need to send the request in this format.

    === Request ===

    <request client_id="1" client_serial_number="abcded" version="0.1">
    <call name="test.reverse">abc</call>
    </request>

    This is the responce that i should get if all goes well

    === Response ===

    <response version="0.1">
    <value>cba</value>
    </response>

    For the header request part, i am using the following code

    Qt Code:
    1. QXmlStreamReader xml;
    2. QHttp http;
    3.  
    4. void senddata::fetch()
    5. {
    6.  
    7. QString username = "abc";
    8. QString password = "12345";
    9.  
    10. xml.clear();
    11.  
    12. QUrl url(lineEdit->text());
    13.  
    14. http.setHost(url.host());
    15. http.setUser(username,password);
    16. connectionId = http.get(url.path());
    17. }
    To copy to clipboard, switch view to plain text mode 
    I am reading the data here

    Qt Code:
    1. void senddata::readData(const QHttpResponseHeader &resp)
    2. {
    3. if (resp.statusCode() != 200)
    4. http.abort();
    5. else {
    6. xml.addData(http.readAll());
    7. parseXml();// This funtion has nothing in it as of now
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 

    Is my code correct? How should i proceed?

    How can i parse the response xml and get the data i need?

  2. #2
    Join Date
    Feb 2009
    Posts
    143
    Thanks
    8

    Default Re: Reqest and Response in XML

    can somebody help me with this??

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Reqest and Response in XML

    For such simple things I would use QDomDocument instead of the stream reader.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Feb 2009
    Posts
    143
    Thanks
    8

    Default Re: Reqest and Response in XML

    thanks for replying mate.

    I think what u suggested is what i need for proper functioning

    abt headers and data, how can I send the data along with the header? I am stuck here.

    Can QDomDocument also manage the response as well as the QHttpResponseHeader?

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Reqest and Response in XML

    QDomDocument is for parsing xml data, it has nothing to do with HTTP.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Feb 2009
    Posts
    143
    Thanks
    8

    Default Re: Reqest and Response in XML

    thanks mate.

    can somebody guide me as how i can send the data along with my header??

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Reqest and Response in XML

    Did you notice QHttp::post() and QHttp::request()?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Feb 2009
    Posts
    143
    Thanks
    8

    Default Re: Reqest and Response in XML

    ok.

    int QHttp::request ( const QHttpRequestHeader & header, QIODevice * data = 0, QIODevice * to = 0 )

    can you explain the function in a better way, than the explanation that is present in Qt Documentation?

    what should i assign in the QHttpRequestHeader and in QIODevice?

    this is the request header

    Qt Code:
    1. QByteArray string ("username");
    2. string.append(":");
    3. string.append("12345");
    4. QString converted = string.toBase64();
    5. //qDebug() <<"String"<< string<<"converted" <<converted ;
    6.  
    7. QUrl url(lineEdit->text());
    8.  
    9. http.setHost(url.host());
    10.  
    11. connectionId = http.get(url.path());
    To copy to clipboard, switch view to plain text mode 

    this is the data that has to be sent along with the header.

    Qt Code:
    1. void MainWindow::sendrequest()
    2. {
    3. QDomDocument doc("request");
    4. QDomElement root = doc.createElement("request");
    5. doc.appendChild(root);
    6.  
    7. QDomElement tag = doc.createElement("call");
    8. root.appendChild(tag);
    9.  
    10. QDomText t = doc.createTextNode("abc");
    11. tag.appendChild(t);
    12.  
    13. QString xml = doc.toString();
    14. }
    To copy to clipboard, switch view to plain text mode 

    can u help me as how I should call the request function??

    Will using QNetworkAccessManager for the request and response from a webpage, be easier?

    thanks
    Last edited by srohit24; 1st July 2009 at 16:46.

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Reqest and Response in XML

    "data" is a pointer to a QIODevice (such as QBuffer) containing the data the engine should send to the server as the request (i.e. the result of your sendrequest() method). As for the header - the least you have to provide is the method (GET or POST) and relative path of the URL that should be queried.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. The following user says thank you to wysota for this useful post:

    srohit24 (1st July 2009)

  11. #10
    Join Date
    Feb 2009
    Posts
    143
    Thanks
    8

    Default Re: Reqest and Response in XML

    thanks mate.

    so i need to call the request, like this?
    http.request( GET, xmlrequest);

    I need to have the xml request like this

    <request client_id="1" client_serial_number="abcded" version="0.1">
    <call name="test.reverse">abc</call>
    </request>

    but after using this code,

    Qt Code:
    1. QDomDocument doc("request");
    2. QDomElement root = doc.createElement("request");
    3. doc.appendChild(root);
    4.  
    5. QDomElement tag = doc.createElement("call");
    6. QString name = "teci.test.reverse";
    7. QString value = "abc";
    8. tag.setAttribute(name,value);
    9. root.appendChild(tag);
    10.  
    11. QString xmlrequest = doc.toString();
    To copy to clipboard, switch view to plain text mode 

    i am able to only this.

    "<!DOCTYPE request>
    <request>
    <call test.reverse="abc" />
    </request>

    can you help me with this?

  12. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Reqest and Response in XML

    Quote Originally Posted by srohit24 View Post
    thanks mate.

    so i need to call the request, like this?
    http.request( GET, xmlrequest);
    Not really, you have to match the argument types (and values of course).

    I need to have the xml request like this

    <request client_id="1" client_serial_number="abcded" version="0.1">
    <call name="test.reverse">abc</call>
    </request>

    but after using this code,

    Qt Code:
    1. QDomDocument doc("request");
    2. QDomElement root = doc.createElement("request");
    3. doc.appendChild(root);
    4.  
    5. QDomElement tag = doc.createElement("call");
    6. QString name = "teci.test.reverse";
    7. QString value = "abc";
    8. tag.setAttribute(name,value);
    9. root.appendChild(tag);
    10.  
    11. QString xmlrequest = doc.toString();
    To copy to clipboard, switch view to plain text mode 

    i am able to only this.

    "<!DOCTYPE request>
    <request>
    <call test.reverse="abc" />
    </request>
    What more would you expect from the above code snippet?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. #12
    Join Date
    Feb 2009
    Posts
    143
    Thanks
    8

    Default Re: Reqest and Response in XML

    i want to exact copy of the request xml thats been given.

    I am asking, what changes should i make to get the xml that will request the server.

  14. #13
    Join Date
    Feb 2009
    Posts
    143
    Thanks
    8

    Default Re: Reqest and Response in XML

    i have made some changes to my code

    I first need to get the Client id and the serial no

    Qt Code:
    1. //This is how i need to send the request
    2. === Request ===
    3.  
    4. <request version="0.1">
    5. <call name="client.create">
    6. <client>
    7. <kind>MOBILE</kind>
    8. <name>API Test suite 7936</name>
    9. </client>
    10. </call>
    11. </request>
    12.  
    13. //This is what i will get, if everything goes well
    14.  
    15. === Response ===
    16.  
    17. <response version="0.1">
    18. <client id="1">
    19. <name>API Test suite 7936</name>
    20. <kind>MOBILE</kind>
    21. <serial_number>a09b4d3b-08b7-4cdf-bf3c-a5a8fb27ee1f</serial_number>
    22. <added_on>2009-06-25 12:33:49.182287</added_on>
    23. <enabled>True</enabled>
    24. </client>
    25. </response>
    To copy to clipboard, switch view to plain text mode 


    My code to send the request is this

    Qt Code:
    1. void MainWindow::fetch()
    2. {
    3.  
    4. QByteArray string ("username");
    5. string.append(":");
    6. string.append("12345");
    7. QString converted = string.toBase64();
    8.  
    9. xml.clear();
    10.  
    11. QUrl url("http://something/");
    12.  
    13. http.setHost(url.host(),url.port(80));
    14. http.setUser(converted);
    15. connectionId = http.get(url.path());
    16.  
    17. sendrequest();
    18. http.post(url.path(),xmlrequest);
    19.  
    20. }
    21.  
    22. void MainWindow::sendrequest()
    23. {
    24. QDomElement root = doc.createElement("request");
    25. root.setAttribute("version", "0.1");
    26. doc.appendChild(root);
    27.  
    28. QDomElement tag = doc.createElement("call");
    29. tag.setAttribute("name","teci.client.create");
    30. root.appendChild(tag);
    31.  
    32. QDomElement tag1 = doc.createElement("client");
    33. tag.appendChild(tag1);
    34.  
    35. QDomElement tag2 = doc.createElement("kind");
    36. QDomText t = doc.createTextNode("MOBILE");
    37. tag2.appendChild(t);
    38. tag1.appendChild(tag2);
    39.  
    40. QDomElement tag3 = doc.createElement("name");
    41. t = doc.createTextNode("API Test suite 7936");
    42. tag3.appendChild(t);
    43. tag1.appendChild(tag3);
    44.  
    45. xmlrequest.append(doc.toString());
    46. qDebug() << xmlrequest.data();
    47. }
    To copy to clipboard, switch view to plain text mode 

    output i am getting is this

    Qt Code:
    1. <request version="0.1" >
    2. <call name="client.create" >
    3. <client>
    4. <kind>MOBILE</kind>
    5. <name>API Test suite 7936</name>
    6. </client>
    7. </call>
    8. </request>
    9.  
    10. Received error during HTTP fetch.
    To copy to clipboard, switch view to plain text mode 

    What should I do to get the program working?

    The request is according to the specification. What am i missing here?

  15. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Reqest and Response in XML

    Why are you sending both get and post? And how do you process the responses?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  16. #15
    Join Date
    Feb 2009
    Posts
    143
    Thanks
    8

    Default Re: Reqest and Response in XML

    i have a function to read the xml and then to parse it.

    but as i am getting a error during fetch, i didnt show the code.

    abt both get and post, i am not sure which one I should be using. so i was trying both of them

  17. #16
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Reqest and Response in XML

    You should use POST if you want to send some data to the server.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  18. #17
    Join Date
    Feb 2009
    Posts
    143
    Thanks
    8

    Default Re: Reqest and Response in XML

    ok. i have made the change ( using http.post now)

    but still the same problem

    Received error during HTTP fetch.

    I have the following code to read the data and to check if the server has finished sending

    Qt Code:
    1. void RSSListing::readData(const QHttpResponseHeader &resp)
    2. {
    3. qDebug() << "XML"<<http.readAll();
    4. if (resp.statusCode() != 200)
    5. http.abort();
    6. else {
    7. xml.addData(http.readAll());
    8. parseXml();
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void RSSListing::finished(int id, bool error)
    2. {
    3. if (error) {
    4. qWarning("Received error during HTTP fetch.");
    5. lineEdit->setReadOnly(false);
    6. abortButton->setEnabled(false);
    7. fetchButton->setEnabled(true);
    8. }
    9. else if (id == connectionId) {
    10. lineEdit->setReadOnly(false);
    11. abortButton->setEnabled(false);
    12. fetchButton->setEnabled(true);
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 

  19. #18
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Reqest and Response in XML

    And when exactly does the error occur?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  20. #19
    Join Date
    Feb 2009
    Posts
    143
    Thanks
    8

    Default Re: Reqest and Response in XML

    I have a UI, which has 2 buttons and a text area

    When i click on the 1st button, the fetch function is called.
    The 2nd button is abort

    When i click on the fetch button, the http sends the data.

    But i am not able to receive anything from the server

  21. #20
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Reqest and Response in XML

    Try to get more details about the request/response that triggers the error. What does QHttp::errorString() return?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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.