How to debug a QNetworkRequest with a QHttpMultiPart posted by QNetworkAccessManager?
Solved
General and Desktop
-
Hi,
I am new to using QNetwork* things. I am trying to use an API and I am not sure, what the actual output of my QNetworkRequest looks like. (Qt 5.15.2, Windows)m_manager->post(QNetworkRequest(url), multiPart); qDebug()<<multiPart;
doesn't show any content.
In case it might be helpful, here's my code so far.
In the class constructor I create an instance for the Manager and connect it to a reply slot:m_manager = new QNetworkAccessManager(); connect(m_manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
The reply slot is simple:
void MainWindow::replyFinished(QNetworkReply *reply) { qDebug() << "request finshed, reply receveid"; qDebug() << reply->error(); QByteArray data=reply->readAll(); qDebug() << data; }
Then I assemble a request:
void MainWindow::sendARequest() { QString productCode = "7374698765439"; QString fieldContent = "MyFoodNameForTestingReasons"; if(!m_manager) { qDebug() << "no network manager avaible"; return; } //QUrl url("http://off:off@world.openfoodfacts.net/cgi/product_jqm2.pl"); QUrl url("http://world.openfoodfacts.net/cgi/product_jqm2.pl"); QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType); QHttpPart barcodePart; barcodePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"code\"")); barcodePart.setBody(productCode.toUtf8()); QHttpPart usernamePart; usernamePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"user_id\"")); usernamePart.setBody(QString("off").toUtf8()); QHttpPart passwordPart; passwordPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"password\"")); passwordPart.setBody(QString("off").toUtf8()); QHttpPart fieldPart; fieldPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"product_name\"")); fieldPart.setBody(fieldContent.toUtf8()); multiPart->append(barcodePart); multiPart->append(usernamePart); multiPart->append(passwordPart); multiPart->append(fieldPart); /// HERE I'd like to know the actual "outgoing" request, just as it is going to be posted. qDebug()<<multiPart; m_manager->post(QNetworkRequest(url), multiPart); }
-
Hi,
You are getting a pointer value since your QHttpMultPart is allocated on the heap.
You can use a tool like Wireshark to inspect what is going on or a service like RequestBin to inspect your request.