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. QNetworkAccessManager returning empty results
Forum Update on Monday, May 27th 2025

QNetworkAccessManager returning empty results

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 2.5k Views
  • 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.
  • R Offline
    R Offline
    rejoraj
    wrote on last edited by
    #1

    Hi all,
    I am struggling with qnetworkaccessmanager for quite sometime. I googled a lot, but I donot find a solution for this.

    I am creating a client using qaccessmanager to talk with a rest server. QNetworkReply is not returning any results. The server is working properly but the client is not returning results. On top of that the server gets called 3 times and sometimes the server is crashing. Hope some one can figure out what is going wrong. I am attaching the client code. Also the ready read is not firing. The readyread is properly connected to the slot.

    I tried different approches like connecting finished signal of networkaccessmanager, qnetworkreply e.t.c. But all of them ends up in giving the same error "Connection Closed" or the readAll bytearray being empty.

    @void RestClientCore::ConnectToServer()
    {
    m_NetworkManager = new QNetworkAccessManager(this);

    QUrl url("http://localhost");
    url.setPort(5432);
    QByteArray postData;
     postData.append("/?userid=user");
     postData.append("&site=site");
    QNetworkReply *reply = m_NetworkManager->post(request,postData);
    connect(reply, SIGNAL(readyRead()),this, SLOT(slotReadyRead()));
    connect(reply, SIGNAL(finished()), this, SLOT(onRequestCompleted()));
    

    }

    void RestClientCore::onRequestCompleted() {

    QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());
    if(reply->error())
    {
    qDebug() <<reply->bytesAvailable() << reply->errorString();
    }
    else
    {
    qDebug() << reply->readAll();
    }
    reply->deleteLater();
    }

    void RestClientCore::slotReadyRead()
    {
    QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());
    qDebug() << reply->readAll();
    }@

    Thanks in advance
    Regards
    Rejo Raj

    1 Reply Last reply
    0
    • R Offline
      R Offline
      RavensAngel
      wrote on last edited by
      #2

      What is request in line 10:
      @
      QNetworkReply *reply = m_NetworkManager->post(request,postData);
      @

      I dont' think you need the readyRead signal, and you can check the isFinished() method on reply finished (in the onRequestCompleted.

      Connect the error signal, maybe it gives you more details on what is going on.

      1 Reply Last reply
      0
      • R Offline
        R Offline
        rejoraj
        wrote on last edited by
        #3

        I am sorry. I forgot to add the details of the request. I am adding the code. The error signal is there in my actual implementation. It just says Connection closed. Nothing other than that.

        I used the reply->isFinished() as you said. Still the reply is emty there and the server is calling thrice. First time correctly and the second and third time with empty post requests

        @void RestClientCore::ConnectToServer()
        {
        m_NetworkManager = new QNetworkAccessManager(this);

        QUrl url("http://localhost");
        url.setPort(5432);
        

        QNetworkRequest request(url);
        request.setHeader(QNetworkRequest::ContentTypeHeader,
        "application/x-www-form-urlencoded");

        QByteArray postData;
         postData.append("/?userid=user");
         postData.append("&site=site");
        QNetworkReply *reply = m_NetworkManager->post(request,postData);
        connect(reply, SIGNAL(readyRead()),this, SLOT(slotReadyRead()));
        connect(reply, SIGNAL(finished()), this, SLOT(onRequestCompleted()));
        

        }

        void RestClientCore::onRequestCompleted()
        {

        QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());
        if(reply->error())
        {
        qDebug() <<reply->bytesAvailable() << reply->errorString();
        }
        if(reply->isFinished())
        {
        qDebug() << "Reply Finished" << reply->readAll();
        reply->deleteLater();
        }
        }

        void RestClientCore::slotReadyRead()
        {
        QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());
        qDebug() << reply->readAll();
        }
        @

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          @m_NetworkManager = new QNetworkAccessManager(this);@

          Should be in RestClientCore constructor. You are creating a new QNAM each time you call ConnectToServer and never delete the old one.

          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
          0

          • Login

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