PDA

View Full Version : Correct POST html with JSON data



Mbded
18th August 2015, 01:06
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.


<html>
<body>
<form method="POST" action="http://192.168.1.108/ask.cgi">
<input type="text" name="json"
value='{"ctrl":{"c":"gd","i":2}}' size="100">
<input type="submit">
</form>
</body>
</html>

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.


QByteArray jsonString = "{\"trl\":{\"c\":\"gd\",\"i\":256}}";
QByteArray postDataSize = QByteArray::number(jsonString.size());

QUrl serviceURL("http://192.168.1.108/ask.cgi/");
QNetworkRequest request(serviceURL);
request.setRawHeader("Content-Type", "application/x-www-form-urlencoded");
request.setRawHeader("Content-Length", postDataSize);

QNetworkAccessManager test;
QEventLoop loop;
connect(&test, SIGNAL(finished(QNetworkReply*)), &loop, SLOT(quit()));
QNetworkReply * reply = test.post(request, jsonString);
loop.exec();

QByteArray response = reply->readAll();
qd " slot" << response;

anda_skoa
18th August 2015, 09:45
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,
_

Mbded
18th August 2015, 11:08
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.network.ssl: QSslSocket: cannot resolve SSlv2_client_method
qt.network.ssl: QSslSocket: cannot resolve SSlv2_server_method

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

Mbded
18th August 2015, 21:36
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:


QByteArray response = reply->readAll();
qd " slot" << response << reply->error();

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 ?

anda_skoa
18th August 2015, 22:06
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,
_

Mbded
18th August 2015, 22:31
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.

ChrisW67
18th August 2015, 22:43
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.

Mbded
19th August 2015, 09:49
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

ChrisW67
19th August 2015, 22:11
The body of the POST request from the form will probably look like a variation on this:


json=%7B%22ctrl%22%3A%7B%22c%22%3A%22gd%22%2C%22i% 22%3A2%7D%7D

What your C++ code sends is a body:


{"ctrl":{"c":"gd","i":256}}

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.

Mbded
22nd August 2015, 11:28
Ok I did something what can help.

In my browser I writed this


http://192.168.1.109/sdk.cgi?json={"ctrl":{"c":"gd","i":256}}

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


Internet Protocol Version 4, Src: 192.168.1.100 (192.168.1.100), Dst: 192.168.1.109 (192.168.1.109)
Version: 4
Header Length: 20 bytes
Differentiated Services Field: 0x00 (DSCP 0x00: Default; ECN: 0x00: Not-ECT (Not ECN-Capable Transport))
Total Length: 506
Identification: 0xd1e4 (53732)
Flags: 0x02 (Don't Fragment)
Fragment offset: 0
Time to live: 64
Protocol: TCP (6)
Header checksum: 0xe2f7 [validation disabled]
Source: 192.168.1.100 (192.168.1.100)
Destination: 192.168.1.109 (192.168.1.109)
[Source GeoIP: Unknown]
[Destination GeoIP: Unknown]
Transmission Control Protocol, Src Port: 34863 (34863), Dst Port: 80 (80), Seq: 1, Ack: 1, Len: 454
Hypertext Transfer Protocol
GET /ask.cgi?json={%22ctrl%22:{%22c%22:%22gd%22,%22i%22 :256}} HTTP/1.1\r\n
[Expert Info (Chat/Sequence): GET /ask.cgi?json={%22ctrl%22:{%22c%22:%22gd%22,%22i%22 :256}} HTTP/1.1\r\n]
[GET /ask.cgi?json={%22ctrl%22:{%22c%22:%22gd%22,%22i%22 :256}} HTTP/1.1\r\n]
[Severity level: Chat]
[Group: Sequence]
Request Method: GET
Request URI: /sdk.cgi?json={%22ctrl%22:{%22c%22:%22gd%22,%22i%22 :256}}
Request Version: HTTP/1.1
Host: 192.168.1.109\r\n
Connection: keep-alive\r\n
Cache-Control: max-age=0\r\n
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8\r\n
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
Accept-Encoding: gzip, deflate, sdch\r\n
Accept-Language: pl-PL,pl;q=0.8,en-US;q=0.6,en;q=0.4\r\n
\r\n
[Full request URI: http://192.168.1.109/ask.cgi?json={json={%22ctrl%22:{%22c%22:%22gd%22,% 22i%22:256}}]
[HTTP request 1/1]
[Response in frame: 16]

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 ?

ChrisW67
23rd August 2015, 07:22
I have already told you how to build a suitable query.

Mbded
23rd August 2015, 11:19
Solved :)

Is very easy. Thank You for all helpful reply



QUrl url("http://192.168.1.109/ask.cgi");
QUrlQuery query;
query.setQuery("json={\"ctrl\":{\"c":\"gd\",\"i\":6}}");
url.setQuery(query);
QNetworkRequest req(url);

QNetworkAccessManager test;

QEventLoop loop;
connect(&test, SIGNAL(finished(QNetworkReply*)), &loop, SLOT(quit()));
QNetworkReply * reply = test.get(req);
loop.exec();

Then we get answer ;)