Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to debug a QNetworkRequest with a QHttpMultiPart posted by QNetworkAccessManager?

How to debug a QNetworkRequest with a QHttpMultiPart posted by QNetworkAccessManager?

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 822 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • SeDiS Offline
    SeDiS Offline
    SeDi
    wrote on last edited by SeDi
    #1

    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);
            }
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved