Results 1 to 12 of 12

Thread: Correct POST html with JSON data

  1. #1
    Join Date
    Aug 2015
    Posts
    7
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Correct POST html with JSON data

    Hello

    I have problem with send POST method to server with JSON data. Actualy I tryed many idea, also idea with this forum but nothing helped me. So finally I whant to will ask in this forum. Basicly I have html file which below content which working.

    Qt Code:
    1. <html>
    2. <body>
    3. <form method="POST" action="http://192.168.1.108/ask.cgi">
    4. <input type="text" name="json"
    5. value='{"ctrl":{"c":"gd","i":2}}' size="100">
    6. <input type="submit">
    7. </form>
    8. </body>
    9. </html>
    To copy to clipboard, switch view to plain text mode 

    After I writed this text to let say test.html and execute, my serwer answer me correct, also in JSON data. So please me tell how to build POST method using Qt, and concret using QNetworkManager. I writed below code, but nothing happend. I see in wireshark how program traying something send but I do not get nothing.

    Qt Code:
    1. QByteArray jsonString = "{\"trl\":{\"c\":\"gd\",\"i\":256}}";
    2. QByteArray postDataSize = QByteArray::number(jsonString.size());
    3.  
    4. QUrl serviceURL("http://192.168.1.108/ask.cgi/");
    5. QNetworkRequest request(serviceURL);
    6. request.setRawHeader("Content-Type", "application/x-www-form-urlencoded");
    7. request.setRawHeader("Content-Length", postDataSize);
    8.  
    9. QNetworkAccessManager test;
    10. QEventLoop loop;
    11. connect(&test, SIGNAL(finished(QNetworkReply*)), &loop, SLOT(quit()));
    12. QNetworkReply * reply = test.post(request, jsonString);
    13. loop.exec();
    14.  
    15. QByteArray response = reply->readAll();
    16. qd " slot" << response;
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Correct POST html with JSON data

    Have you checked the error code of the "reply" object.
    Is it a typo in your post that the HTML had "ctrl" in its JSON string but the C++ code has" trl"?

    Cheers,
    _

  3. #3
    Join Date
    Aug 2015
    Posts
    7
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Correct POST html with JSON data

    In above my post not see that, but in orginal my code I apply error raport, and ofcorse I do not get nothing, only empty QByteArray, without raport error. I cheked may program in wireshark and I saw how my program tryed something send do API, but not recive correct content.
    I have forgotten something writed after, probalby I have problem with other thing I get in console warring when I try send POST, content is below:

    Qt Code:
    1. qt.network.ssl: QSslSocket: cannot resolve SSlv2_client_method
    2. qt.network.ssl: QSslSocket: cannot resolve SSlv2_server_method
    To copy to clipboard, switch view to plain text mode 

    I just instaled openssl package (I write this on linux) but it is nothing changes.

    About wrong content JSON request, this is only mistake at wirting this post, but please belive me, this is not important, because If I lunched html file with this same request evrything is works. In my API I also send others command, and evry working with html file.
    I think I have just porblem with correct setup, or liberary on my pc.

    ps Very thanks for reply
    Last edited by Mbded; 18th August 2015 at 11:12.

  4. #4
    Join Date
    Aug 2015
    Posts
    7
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Correct POST html with JSON data

    Sorry for add new post, but I do not saw edit function, but I think I have something important. anda_skoa I interested more error result, so I add to code this line:

    Qt Code:
    1. QByteArray response = reply->readAll();
    2. qd " slot" << response << reply->error();
    To copy to clipboard, switch view to plain text mode 

    and result is nr 2. In my Qt it is mean

    the remote server closed the connection prematurely, before the entire reply was received and processed

    So what now ? How Can I unterstand this answer ?

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Correct POST html with JSON data

    That most likely means that the serve didn't like your request.

    Check the HTTP response code, see QNetworkReply::attribute()

    Since you are already employing a network traffic monitor, check if the browser sends something different then what you do.

    Cheers,
    _

  6. #6
    Join Date
    Aug 2015
    Posts
    7
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Correct POST html with JSON data

    Ok I checked, but could You tell me what is can be important, because I don't know what to look for. Something in HEAD, lenght ? I thought if I show working html file You will be know evrything, but unfortunately but Ok I understand sytuation I thought it is simply as content html file but apparently I was wrong.

  7. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Correct POST html with JSON data

    What your code sends as the request body is a raw JSON string. What your HTML form sends as the body of the POST is not a raw JSON string, it is a name value pair where the string is HTML-encoded. If this is what the server is expecting then you need to mimic that.

  8. #8
    Join Date
    Aug 2015
    Posts
    7
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Correct POST html with JSON data

    Firstly I will show Logs from wireshark, ofcorse this is not all, because content is to big as for put to this post. This is after execute request from html file. Him content is above in my post. So may be now You Can tell me how Can I build request in Qt

    What your code sends as the request body is a raw JSON string
    Can You tell me more ? How Can I do that in Qt ?

    No. Time Source Destination Protocol Length Info
    9 16.442812000 192.168.1.100 192.168.1.108 TCP 74 39008→80 [SYN] Seq=0 Win=29200 Len=0 MSS=1460 SACK_PERM=1 TSval=25654464 TSecr=0 WS=128

    Frame 9: 74 bytes on wire (592 bits), 74 bytes captured (592 bits) on interface 0
    Interface id: 0 (wlan0)
    Encapsulation type: Ethernet (1)
    Arrival Time: Aug 19, 2015 09:53:07.607279000 CEST
    [Time shift for this packet: 0.000000000 seconds]
    Epoch Time: 1439970787.607279000 seconds
    [Time delta from previous captured frame: 2.512966000 seconds]
    [Time delta from previous displayed frame: 0.000000000 seconds]
    [Time since reference or first frame: 16.442812000 seconds]
    Frame Number: 9
    Frame Length: 74 bytes (592 bits)
    Capture Length: 74 bytes (592 bits)
    [Frame is marked: False]
    [Frame is ignored: False]
    [Protocols in frame: eth:ethertype:ip:tcp]
    [Coloring Rule Name: HTTP]
    [Coloring Rule String: http || tcp.port == 80 || http2]
    Ethernet II, Src: HonHaiPr_3b:8d:ce (e4:d5:3d:3b:8d:ce), Dst:xxx_03:0d:f5 (18:cc:23:03:0d:f5)
    Internet Protocol Version 4, Src: 192.168.1.100 (192.168.1.100), Dst: 192.168.1.108 (192.168.1.108)
    Transmission Control Protocol, Src Port: 39008 (39008), Dst Port: 80 (80), Seq: 0, Len: 0

    No. Time Source Destination Protocol Length Info
    10 16.444134000 192.168.1.108 192.168.1.100 TCP 74 80→39008 [SYN, ACK] Seq=0 Ack=1 Win=5792 Len=0 MSS=1460 SACK_PERM=1 TSval=378311 TSecr=25654464 WS=4

    Frame 10: 74 bytes on wire (592 bits), 74 bytes captured (592 bits) on interface 0
    Interface id: 0 (wlan0)
    Encapsulation type: Ethernet (1)
    Arrival Time: Aug 19, 2015 09:53:07.608601000 CEST
    [Time shift for this packet: 0.000000000 seconds]
    Epoch Time: 1439970787.608601000 seconds
    [Time delta from previous captured frame: 0.001322000 seconds]
    [Time delta from previous displayed frame: 0.001322000 seconds]
    [Time since reference or first frame: 16.444134000 seconds]
    Frame Number: 10
    Frame Length: 74 bytes (592 bits)
    Capture Length: 74 bytes (592 bits)
    [Frame is marked: False]
    [Frame is ignored: False]
    [Protocols in frame: eth:ethertype:ip:tcp]
    [Coloring Rule Name: HTTP]
    [Coloring Rule String: http || tcp.port == 80 || http2]
    Ethernet II, Src: xxxx_03:0d:f5 (18:cc:23:03:0d:f5), Dst: HonHaiPr_3b:8d:ce (e4:d5:3d:3b:8d:ce)
    Internet Protocol Version 4, Src: 192.168.1.108 (192.168.1.108), Dst: 192.168.1.100 (192.168.1.100)
    Transmission Control Protocol, Src Port: 80 (80), Dst Port: 39008 (39008), Seq: 0, Ack: 1, Len: 0


    No. Time Source Destination Protocol Length Info
    12 16.444457000 192.168.1.100 192.168.1.108 HTTP 613 POST /ask.cgi HTTP/1.1 (application/x-www-form-urlencoded)

    Frame 12: 613 bytes on wire (4904 bits), 613 bytes captured (4904 bits) on interface 0
    Interface id: 0 (wlan0)
    Encapsulation type: Ethernet (1)
    Arrival Time: Aug 19, 2015 09:53:07.608924000 CEST
    [Time shift for this packet: 0.000000000 seconds]
    Epoch Time: 1439970787.608924000 seconds
    [Time delta from previous captured frame: 0.000284000 seconds]
    [Time delta from previous displayed frame: 0.000284000 seconds]
    [Time since reference or first frame: 16.444457000 seconds]
    Frame Number: 12
    Frame Length: 613 bytes (4904 bits)
    Capture Length: 613 bytes (4904 bits)
    [Frame is marked: False]
    [Frame is ignored: False]
    [Protocols in frame: eth:ethertype:ip:tcp:http:urlencoded-form]
    [Number of per-protocol-data: 1]
    [Hypertext Transfer Protocol, key 0]
    [Coloring Rule Name: HTTP]
    [Coloring Rule String: http || tcp.port == 80 || http2]
    Ethernet II, Src: HonHaiPr_3b:8d:ce (e4:d5:3d:3b:8d:ce), Dst: xxx_03:0d:f5 (18:cc:23:03:0d:f5)
    Internet Protocol Version 4, Src: 192.168.1.100 (192.168.1.100), Dst: 192.168.1.108 (192.168.1.108)
    Transmission Control Protocol, Src Port: 39008 (39008), Dst Port: 80 (80), Seq: 1, Ack: 1, Len: 547
    Hypertext Transfer Protocol
    HTML Form URL Encoded: application/x-www-form-urlencoded
    Last edited by Mbded; 19th August 2015 at 10:16.

  9. #9
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Correct POST html with JSON data

    The body of the POST request from the form will probably look like a variation on this:
    Qt Code:
    1. json=%7B%22ctrl%22%3A%7B%22c%22%3A%22gd%22%2C%22i%22%3A2%7D%7D
    To copy to clipboard, switch view to plain text mode 
    What your C++ code sends is a body:
    Qt Code:
    1. {"ctrl":{"c":"gd","i":256}}
    To copy to clipboard, switch view to plain text mode 
    Which is a different JSON object (the i value is different), not encoded in the same way, and not accepted by the server.
    You can use QUrlQuery to build a POST body like the form does.

  10. #10
    Join Date
    Aug 2015
    Posts
    7
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Correct POST html with JSON data

    Ok I did something what can help.

    In my browser I writed this

    Qt Code:
    1. http://192.168.1.109/sdk.cgi?json={"ctrl":{"c":"gd","i":256}}
    To copy to clipboard, switch view to plain text mode 

    Ofcorse I get answer form serwer. Then I take wireshark, to look what happend. And:

    Qt Code:
    1. Internet Protocol Version 4, Src: 192.168.1.100 (192.168.1.100), Dst: 192.168.1.109 (192.168.1.109)
    2. Version: 4
    3. Header Length: 20 bytes
    4. Differentiated Services Field: 0x00 (DSCP 0x00: Default; ECN: 0x00: Not-ECT (Not ECN-Capable Transport))
    5. Total Length: 506
    6. Identification: 0xd1e4 (53732)
    7. Flags: 0x02 (Don't Fragment)
    8. Fragment offset: 0
    9. Time to live: 64
    10. Protocol: TCP (6)
    11. Header checksum: 0xe2f7 [validation disabled]
    12. Source: 192.168.1.100 (192.168.1.100)
    13. Destination: 192.168.1.109 (192.168.1.109)
    14. [Source GeoIP: Unknown]
    15. [Destination GeoIP: Unknown]
    16. Transmission Control Protocol, Src Port: 34863 (34863), Dst Port: 80 (80), Seq: 1, Ack: 1, Len: 454
    17. Hypertext Transfer Protocol
    18. GET /ask.cgi?json={%22ctrl%22:{%22c%22:%22gd%22,%22i%22:256}} HTTP/1.1\r\n
    19. [Expert Info (Chat/Sequence): GET /ask.cgi?json={%22ctrl%22:{%22c%22:%22gd%22,%22i%22:256}} HTTP/1.1\r\n]
    20. [GET /ask.cgi?json={%22ctrl%22:{%22c%22:%22gd%22,%22i%22:256}} HTTP/1.1\r\n]
    21. [Severity level: Chat]
    22. [Group: Sequence]
    23. Request Method: GET
    24. Request URI: /sdk.cgi?json={%22ctrl%22:{%22c%22:%22gd%22,%22i%22:256}}
    25. Request Version: HTTP/1.1
    26. Host: 192.168.1.109\r\n
    27. Connection: keep-alive\r\n
    28. Cache-Control: max-age=0\r\n
    29. Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8\r\n
    30. User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.152 Safari/537.36\r\n
    31. Accept-Encoding: gzip, deflate, sdch\r\n
    32. Accept-Language: pl-PL,pl;q=0.8,en-US;q=0.6,en;q=0.4\r\n
    33. \r\n
    34. [Full request URI: http://192.168.1.109/ask.cgi?json={json={%22ctrl%22:{%22c%22:%22gd%22,%22i%22:256}}]
    35. [HTTP request 1/1]
    36. [Response in frame: 16]
    To copy to clipboard, switch view to plain text mode 

    May be now someone help me and tell how to build correct request in qt ? it is important chars \r\n in above query, and I must also using them in query in qt ?

  11. #11
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Correct POST html with JSON data

    I have already told you how to build a suitable query.

  12. #12
    Join Date
    Aug 2015
    Posts
    7
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Correct POST html with JSON data

    Solved

    Is very easy. Thank You for all helpful reply

    Qt Code:
    1. QUrl url("http://192.168.1.109/ask.cgi");
    2. QUrlQuery query;
    3. query.setQuery("json={\"ctrl\":{\"c":\"gd\",\"i\":6}}");
    4. url.setQuery(query);
    5. QNetworkRequest req(url);
    6.  
    7. QNetworkAccessManager test;
    8.  
    9. QEventLoop loop;
    10. connect(&test, SIGNAL(finished(QNetworkReply*)), &loop, SLOT(quit()));
    11. QNetworkReply * reply = test.get(req);
    12. loop.exec();
    To copy to clipboard, switch view to plain text mode 

    Then we get answer

Similar Threads

  1. Send JSON & Key via HTTP POST
    By Poonarge in forum Newbie
    Replies: 3
    Last Post: 21st August 2014, 10:13
  2. Replies: 0
    Last Post: 12th June 2014, 13:37
  3. model/view json data displaying
    By binaural in forum Qt Programming
    Replies: 5
    Last Post: 27th June 2012, 09:13
  4. HTTP Post issue, HTML Works , QT Does Not,
    By georgeky in forum Newbie
    Replies: 8
    Last Post: 10th September 2011, 14:51
  5. Replies: 0
    Last Post: 10th September 2011, 14:38

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.