Results 1 to 2 of 2

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,529
    Thanks
    13
    Thanked 1,575 Times in 1,504 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.

Similar Threads

  1. Again QNetworkAccessManager POST and PHP
    By ZSWASW in forum Qt Programming
    Replies: 2
    Last Post: 2nd March 2014, 19:38
  2. QNetworkAccessManager post
    By januszmk in forum Newbie
    Replies: 2
    Last Post: 13th April 2012, 09:01
  3. POST and QNetworkAccessManager
    By hakermania in forum Newbie
    Replies: 1
    Last Post: 13th February 2011, 00:05
  4. QNetworkAccessManager::post() never returns
    By danc81 in forum Qt Programming
    Replies: 2
    Last Post: 21st October 2009, 09:13
  5. QNetworkAccessManager double post
    By QPlace in forum Qt Programming
    Replies: 1
    Last Post: 11th February 2009, 04: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.