Results 1 to 7 of 7

Thread: QNetworkAccessManager Post Issues

  1. #1
    Join Date
    Apr 2019
    Posts
    1
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default QNetworkAccessManager Post Issues

    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)

    Qt Code:
    1. curl -X POST -d '{"TIMESTAMP2" :{ "Sensor Name": "Test Sensor", "Event Name": "Test Event" } }' \
    2. 'https://HIDDEN.firebaseio.com/Notifications.json'
    To copy to clipboard, switch view to plain text mode 

    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.

    Qt Code:
    1. _networkManager = new QNetworkAccessManager(this);
    2.  
    3. QVariantMap information;
    4. information.insert("Sensor Name", _sensorName);
    5. information.insert("Event Name", _eventName);
    6.  
    7. QVariantMap jsonMap;
    8. jsonMap.insert(_timeString, information);
    9.  
    10. QJsonDocument doc = QJsonDocument::fromVariant(QVariant(jsonMap));
    11. auto jsonString = doc.toJson();
    12.  
    13. QByteArray postDataSize = QByteArray::number(jsonString.size());
    14.  
    15. QNetworkRequest request("https://HIDDEN.firebaseio.com/Notifications.json");
    16.  
    17. request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
    18. request.setRawHeader("Content-Length", postDataSize);
    19. request.setRawHeader("Authorization", _gCloudata->WebKey);
    20.  
    21. QNetworkReply *reply = _networkManager->post(request, jsonString);
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. Reply receieved: ServerDateContent-TypeContent-LengthConnectionAccess-Control-Allow-OriginCache-ControlStrict-Transport-Security
    2. Network Error: 302
    To copy to clipboard, switch view to plain text mode 
    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:
    Qt Code:
    1. curl: (6) Could not resolve host: POST
    To copy to clipboard, switch view to plain text mode 



    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?

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

    Default Re: QNetworkAccessManager Post Issues

    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
    Qt Code:
    1. curl -d '{"TIMESTAMP2" :{ "Sensor Name": "Test Sensor", "Event Name": "Test Event" } }' 'https://HIDDEN.firebaseio.com/Notifications.json'
    To copy to clipboard, switch view to plain text mode 
    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.

  3. #3
    Join Date
    Nov 2019
    Posts
    2
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: QNetworkAccessManager Post Issues

    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?

  4. #4
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    472
    Thanked 70 Times in 66 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QNetworkAccessManager Post Issues

    jsonString is a QByteArray ...

  5. #5
    Join Date
    Nov 2019
    Posts
    2
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: QNetworkAccessManager Post Issues

    Quote Originally Posted by ChristianEhrlicher View Post
    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.

  6. #6
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    472
    Thanked 70 Times in 66 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QNetworkAccessManager Post Issues

    Quote Originally Posted by silviapalara View Post
    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?

  7. #7
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,376
    Thanks
    246
    Thanked 687 Times in 677 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: QNetworkAccessManager Post Issues

    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?
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Again QNetworkAccessManager POST and PHP
    By ZSWASW in forum Qt Programming
    Replies: 2
    Last Post: 2nd March 2014, 20:38
  2. QNetworkAccessManager post
    By januszmk in forum Newbie
    Replies: 2
    Last Post: 13th April 2012, 10:01
  3. POST and QNetworkAccessManager
    By hakermania in forum Newbie
    Replies: 1
    Last Post: 13th February 2011, 01:05
  4. QNetworkAccessManager::post() never returns
    By danc81 in forum Qt Programming
    Replies: 2
    Last Post: 21st October 2009, 10:13
  5. QNetworkAccessManager double post
    By QPlace in forum Qt Programming
    Replies: 1
    Last Post: 11th February 2009, 05:44

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.