Access data sent with post/put from QNetworkReply
-
Hello everyone,
does someone know how to access the data that I sent with a post or put request from the reply?
for example when the finished signal is fired and the connected slot is called how can I access the data that I originally sent?
Whathever::Whathever() { m_nam = new QNetworkAccessManager; connect(m_nam, SIGNAL(finished(QNetworkReply*)), this, SLOT(onFinished(QNetworkReply*))); } void Whathever::testPost() { QNetworkRequest req("http://whatever.com"); QByteArray payload("test=test"); m_nam->post(req, payload); } void Whathever::onFinished(QNetworkReply *reply) { // Here i want to access the data contained in "payload" }
-
@Jiloc What about:
void Whathever::onFinished(QNetworkReply *reply) { // Here i want to access the data contained in "payload" reply->readAll(); }
?
http://doc.qt.io/qt-5/qnetworkreply.html
http://doc.qt.io/qt-5/qnetworkaccessmanager.html
"QNetworkAccessManager has an asynchronous API. When the replyFinished slot above is called, the parameter it takes is the QNetworkReply object containing the downloaded data as well as meta-data (headers, etc.)." -
@Jiloc Please read documentation: http://doc.qt.io/qt-5/qnetworkreply.html#request
-
@jsulm As I wrote 2 posts above ,I already did. The QNetworkRequest doesn't contains the PAYLOAD of the request. In fact when you send a post or put request you set 2 different parameters in the QNetworkAccessManager methods.
-
What about using a lambda? You could capture the payload.
QNetworkAccessManager *m_nam = new QNetworkAccessManager; QNetworkRequest req(QUrl("http://whatever.com")); QByteArray payload("test=test"); QNetworkReply *reply = m_nam->post(req, payload); connect(reply,&QNetworkReply::finished,this,[&](){ qDebug() << payload; qDebug() << reply->readAll(); reply->deleteLater(); });
-
@sneubert Yep it works, good idea! I will use your suggestion but I will leave the question unsolved for now.
I am a bit surprised that there are no "standard" ways to do it, or at least that (it seems like) there is no direct access to that payload from the reply! -
I solved the problem by creating my own QNetworkAccessManager in order to catch the post data in a variable and emit them with a signal:
#ifndef CLSNAMTEST_H
#define CLSNAMTEST_H#include <QNetworkAccessManager>
#include <QNetworkReply>class clsNamTest : public QNetworkAccessManager
{
Q_OBJECT
public:
clsNamTest(QObject * parent = 0)
: QNetworkAccessManager() {}virtual QNetworkReply* createRequest(Operation op, const QNetworkRequest& request, QIODevice *outgoingData =0) { QString strUrl = request.url().toString(); if (strUrl.indexOf("/json?h") != -1) { if (outgoingData !=0) { QByteArray baTest; QString strTest= request.rawHeader(QString("Content-Length").toUtf8()); baTest = outgoingData->peek(strTest.toInt()); emit dataGot(baTest); QString str(baTest); qDebug() << str; } } reply = QNetworkAccessManager::createRequest(op, request, outgoingData); return reply; }
signals:
void dataGot(QByteArray data);private:
QNetworkReply* reply;
};#endif // CLSNAMTEST_H
-
What is wrong with QNetworkReply::request() ?
-
@Christian-Ehrlicher
You have to read earlier post from OP:Jiloc 28 Sep 2017, 15:50
@jsulm As I wrote 2 posts above ,I already did. The QNetworkRequest doesn't contains the PAYLOAD of the request. In fact when you send a post or put request you set 2 different parameters in the QNetworkAccessManager methods.
Whether that's true or not I don't know, but a couple of people seem to think it is....
-
@Christian-Ehrlicher
The problem is, that the QNetworkAccessManager is associated with a QWebView. If the QWebView has received the reply is the QByteArray empty. So I can't read it again with readall() again.I must call the request again, but therefor I need the posted Json Data from the request before, which was automatic generated and called from the QWebView when I log into a browsergame.
-
Still don't understand why you can't get the request from the reply where you get empty data from...
-
@Christian-Ehrlicher
When you make a post or put request you have to pass 2 parameters to the functions, the QNetworkRequest containing the header and the QByteArray containing the payload.From the response you can access to the associated QNetworkRequest, but not to the QByteArray.
Hope it is clearer now!
-
@Jiloc said in Access data sent with post/put from QNetworkReply:
I am a bit surprised that there are no "standard" ways to do it, or at least that (it seems like) there is no direct access to that payload from the reply!
The payload could be quite large, so it doesn't make sense for the reply to contain a copy of the request payload.
If you want a copy, you should just make the copy before posting your request. (From your latest posts, it sounds like you're already doing this)
-
@Jiloc what you said is pretty clear... I think no one read you anwser or try to understand you problem.
I do because I'm in the same case than you : I want to access of the request's payload that is not availaible in the QNetworkRequest.
For people : readAll on QNetworkReply read the payload of the reply not the request.Unfortunately no way to get the request payload from the reply or reply->request() object => It's up to you to keep it when sending it and associate this payload to the reply.