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'
curl -X POST -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
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();
QNetworkRequest request("https://HIDDEN.firebaseio.com/Notifications.json");
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
request.setRawHeader("Content-Length", postDataSize);
request.setRawHeader("Authorization", _gCloudata->WebKey);
QNetworkReply *reply = _networkManager->post(request, jsonString);
_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::ContentTypeHeader, "application/json");
request.setRawHeader("Content-Length", postDataSize);
request.setRawHeader("Authorization", _gCloudata->WebKey);
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:
Reply receieved: ServerDateContent-TypeContent-LengthConnectionAccess-Control-Allow-OriginCache-ControlStrict-Transport-Security
Network Error: 302
Reply receieved: ServerDateContent-TypeContent-LengthConnectionAccess-Control-Allow-OriginCache-ControlStrict-Transport-Security
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:
curl: (6) Could not resolve host: POST
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?
Bookmarks