Why the post method doesn't send the json to API Rest in QT5?
-
@raven-worx
I tried to do another project where there is just main.cpp and where do#include <QCoreApplication> #include <QNetworkAccessManager> #include <QNetworkReply> #include <QNetworkRequest> #include <QDebug> #include <QObject> #include <QByteArray> #include <QUrl> void replyFinished(QNetworkReply *reply) { reply->deleteLater(); qDebug() << "reply delete!"; qDebug() << "https post_request done!"; } int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QNetworkAccessManager *manager = new QNetworkAccessManager(); QNetworkRequest request(QUrl ("https://cryptic-reaches-94837.herokuapp.com/data/")); request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json"); QObject::connect(manager, &QNetworkAccessManager::finished, replyFinished); quint8 speed = 0x12; quint8 acceleration = 0x5b; QString json = QString("{\"speed\":\"%1\",\"acceleration\":\"%2\"}").arg(speed).arg(acceleration); manager->post(request, json.toUtf8()); return a.exec(); }
and it's work!!!
but how come if add this piece of code to my project, why does not work to me?@trip
but thats a different URL now?!
Check the returned response error again. -
wrote on 21 Oct 2016, 07:27 last edited by
@raven-worx
not only here I left explicit -
wrote on 21 Oct 2016, 14:11 last edited by
@raven-worx
I think the problem of my code is that networking events are not processed by QCoreApplication :: exec () and then, because the execution is always nested in the cycles and never returns to the main unless there is an error , I do not run networking events. How can I solve this problem?
21/23