PDA

View Full Version : QNetworkAccessManager Post Issues



Terryn
24th April 2019, 04:42
Hello,

I've been doing a lot of interfacing with Google Firebase over QNetworkAccessManager, and the only thing I have not been able to get working with this now is posting to the Realtime Database.

I have the following curl script that works (My project's url is hidden for obvious reasons)


curl -X POST -d '{"TIMESTAMP2" :{ "Sensor Name": "Test Sensor", "Event Name": "Test Event" } }' \
'https://HIDDEN.firebaseio.com/Notifications.json'

I am trying to replicate it within the QNetworkAccessManager and i've got the following to build / setup the code. I've had to cobble the sample code below together from a few abstracted values, but it should get the gist across.



_networkManager = new QNetworkAccessManager(this);

QVariantMap information;
information.insert("Sensor Name", _sensorName);
information.insert("Event Name", _eventName);

QVariantMap jsonMap;
jsonMap.insert(_timeString, information);

QJsonDocument doc = QJsonDocument::fromVariant(QVariant(jsonMap));
auto jsonString = doc.toJson();

QByteArray postDataSize = QByteArray::number(jsonString.size());

QNetworkRequest request("https://HIDDEN.firebaseio.com/Notifications.json");

request.setHeader(QNetworkRequest::ContentTypeHead er, "application/json");
request.setRawHeader("Content-Length", postDataSize);
request.setRawHeader("Authorization", _gCloudata->WebKey);

QNetworkReply *reply = _networkManager->post(request, jsonString);


Now when the response comes back i'm printing both the headers and the error code (QNetworkReply::error()) and I am getting the following:


Reply receieved: ServerDateContent-TypeContent-LengthConnectionAccess-Control-Allow-OriginCache-ControlStrict-Transport-Security
Network Error: 302

The top stuff is a complete and utter mystery, but the 302 error code is something that can be followed.
302 is the following: https://tools.ietf.org/html/rfc7231#section-6.3.4

When i do the curl script without the -X in front, i get something Similar:

curl: (6) Could not resolve host: POST




I am guessing the -X is the culprit to this WORKING in a curl command. So I guess my first question is how do I force the network request to run with this -X command?

ChrisW67
25th April 2019, 01:32
The curl request should not need the -X POST (--request POST) option because the -d (--data) option should already cause a POST request to go to the server. I would expect that this works without the curl error message you describe

curl -d '{"TIMESTAMP2" :{ "Sensor Name": "Test Sensor", "Event Name": "Test Event" } }' 'https://HIDDEN.firebaseio.com/Notifications.json'

Calling QNetworkAccessManager::post() will have the same effect.

I do not believe you need to explicitly set the Content-Length header: QNetworkAccessManager::post() can work this out for itself from the QByteArray argument.

I cannot see that the curl command changes the Content-Type header to 'application/json' from the default 'application/x-www-form-urlencoded' as your C++ does.
The curl command also does not set an "Authorization" header. These are two differences between your curl request and the C++ code.

The 302 response is an HTTP redirect (and it came with all the response headers mashed together in your debug output). QNetworkRequest will not follow that automatically, but neither will curl as far as I can see.

silviapalara
30th November 2019, 03:27
I have noticed the documentation for QNetworkAccessManager method post for qt5 does not match what is discussed in this thread. I am also trying to post a Json request and according to
https://doc.qt.io/qt-5/qnetworkaccessmanager.html
post takes 2 arguments: one is the QNetworkRequest and the other is the destination (QIODevice or QByteArray).
So I am curious what version you are using, and whether the api has changed for qt5, in which case how is this functionality achieved now?

ChristianEhrlicher
30th November 2019, 11:07
jsonString is a QByteArray ...

silviapalara
30th November 2019, 19:41
jsonString is a QByteArray ...

If you have an answer you can just come out and say it, I don't really have time to play the "figure it out from the breadcrumbs" game.

ChristianEhrlicher
30th November 2019, 20:09
If you have an answer you can just come out and say it, I don't really have time to play the "figure it out from the breadcrumbs" game.


I already told you what you asked - you wanted to know which function was used. Since jsonString is a QByteArray I would guess the one which takes a QByteArray is used, or?

d_stranz
3rd December 2019, 22:41
I don't really have time to play the "figure it out from the breadcrumbs" game.

Well, then maybe you should post your questions to a forum where you can pay someone to write your code for you, instead of this forum, where volunteers offer up their time and knowledge to help you learn to help yourself reach a higher level of proficiency in Qt.

The Qt5 doc you linked to shows three different post() versions, only one of which takes a QByteArray argument. Having learned from the answer you received that a jsonString -is- a QByteArray, you couldn't go back to that doc and determine the method that fit, but instead you felt that because you weren't spoon-fed the answer you needed to post a snarky reply?