QNetworkAccessManager post() not sending body
-
Yeah I found this post here: QNetworkAccessManager is not sending data part of POST request, but it doesn't apply to my case.
QNetorkAccessManager nam;//static member of a class - shouldn't go out of scope at all
QByteArray json="{...}";//Generated every time
QNetworkRequest req(QUrl("http://authority/..."));//URL rotates periodically with requests
req.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
QNetworkReply *rep=nam.post(req, json);
//connect()ing signalsOn Windows Qt5.12, everything looks fine. But on Linux Qt5.6.1, after 9 requests, server logs and Wireshark tell me that nam isn't sending the json body.
Tried copying the post body QByteArray to a new-ed QBuffer (lest it gets destructed), but that didn't change anything. -
QNetorkAccessManager nam;//static member of a class - shouldn't go out of scope at all
Static? Why?
But on Linux Qt5.6.1,
Such an old version. Reason?
The rest of your post provides nothing that can be used to reproduce your problem. You are claiming that code like this does not work:
#include <QCoreApplication> #include <QDebug> #include <QObject> #include <QNetworkAccessManager> #include <QNetworkReply> #include <QByteArray> class Sender: public QObject { Q_OBJECT public: explicit Sender(QObject *p = nullptr): QObject(p) { m_nam = new QNetworkAccessManager(this); m_rep = nullptr; } ~Sender() {} void send() { QByteArray json(R"( {"title": "foo", "body": "bar", "userId": "45678" } )"); QNetworkRequest req(QUrl("http://jsonplaceholder.typicode.com/posts")); req.setHeader(QNetworkRequest::ContentTypeHeader, "application/json; charset=UTF-8"); m_rep = m_nam->post(req, json); connect(m_rep, &QNetworkReply::finished, this, &Sender::handleFinished); connect(m_rep, &QNetworkReply::errorOccurred, this, &Sender::handleError); } private slots: void handleFinished() { qDebug() << Q_FUNC_INFO << m_rep->readAll(); } void handleError(QNetworkReply::NetworkError code) { qDebug() << Q_FUNC_INFO << code; } private: QNetworkAccessManager *m_nam; QNetworkReply *m_rep; }; int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); Sender sender; sender.send(); return a.exec(); } #include "main.moc"
The POST data quite happily goes out and a response comes back for me (Qt 5.15 on Linux).
How about with your ancient version of Qt? -
@ChrisW67
static: to ensure it doesn't destruct.
5.6.1: it's the preinstalled version for my distro.
Scenario is I'm inventing some auto-web-requestor. On the first request I post to one URL to fetch some data, then in the finished() signal I QTimer::singleShot(0, &PostNextRequest) with prcocessed result of the previous reply. All the request URLs are on one same server, with paths diffe. For request queues longer than 9, the 9th post body doesn't show on the wire, and the server complains about not receiving request body. Also, QNetworkReply::error(NetworkError) doesn't fire on the abortion of 9th request.
The exact same code on Windows Qt5.12 works w/out any problem, so I'm guessing it a bug in my distro's Qt. -
@candy76041820 said in QNetworkAccessManager post() not sending body:
5.6.1: it's the preinstalled version for my distro.
Qt 5.6 was released in 2016. What distro are you currently using which supplies/expects you to use that?
If you are using 5.12 elsewhere you may be sorely disappointed at 5.6 bugs or missing features. Everybody here would advise you to move on from 5.6.