QNetworkAccessManager doesn't send body



  • I can't figure out why sometimes QNetworkAccessManager doen't send body,
    it's a very strange. It happens if I quickly press button send request.

    void Client::usernameFindRequest(const QString &username)
    {
        QJsonObject userObject;
        userObject["username"] = username;
    
        QNetworkRequest request(url + "forgotPassword/username");
        request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
        QNetworkReply *reply = manager->post(request, QJsonDocument(userObject).toJson(QJsonDocument::Compact));
        connect(reply, SIGNAL(finished()),this, SLOT(usernameFindReply()));
    }
    
    void Client::usernameFindReply()
    {
        QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());
        qDebug() << "TeST";
    
        if (reply->error() == QNetworkReply::NoError)
        {
          qDebug() << "OK!";
          QByteArray httpReply = reply->readAll();
          qDebug() << httpReply;
          emit usernameFindStatus(true);
        }
        else
        {
            qDebug() << reply->errorString();
            emit usernameFindStatus(false);
        }
        manager->clearAccessCache();
        reply->deleteLater();
    }
    

    0_1558042102774_Screenshot from 2019-05-17 00-27-16.png


  • Lifetime Qt Champion

    Hi,

    Are you sure all the requests are getting processed ?

    QNetworkAccessManager processes up to 6 request in parallel. Others are queue.

    By the way, where are you sending the request at ?



  • @SGaist I send it on my http server which send me reply.

    void HttpController::run()
    {
      socket = new QTcpSocket();
    
      if (!socket->setSocketDescriptor(this->socketDescriptor) )
      {
        emit error (socket->error());
        return;
      }
    
      //connect(socket, SIGNAL(connected()), this, SLOT(disconnected()), Qt::DirectConnection);
      connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()),Qt::DirectConnection);
      connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()), Qt::DirectConnection);
    
      exec();
    }
    
    
    void HttpController::readyRead()
    {
      if(socket->bytesAvailable())
      {
          //handle and parse are my functions which procees http request and send reply
          handle(parse(socket->readAll()));
          socket->disconnectFromHost();
      }
      else
      {
          qDebug() << "could not receive data";
          socket->close();
      }
    }
    
    
    void HttpController::disconnected()
    {
      socket->deleteLater();
      exit(0);
    }
    
    void HttpController::badRequest() const
    {
        QString reply;
    
        QLocale locale(QLocale::English);
        QDateTime currTime = QDateTime::currentDateTimeUtc();
        QString time = "Date: " + locale.toString(currTime, "ddd, ") + locale.toString(currTime, "d MMM yyyy HH:mm:ss") + " GMT";
    
        reply = "HTTP/1.1 400 Bad Request\r\n%1\r\n";
    
        socket->write(reply.arg(time).toLocal8Bit());
        socket->flush();
        socket->waitForBytesWritten();
    }
    
    void HttpController::authenticationReply(const QString &username, const QString &password)
    {
        bool ok = storage->authentication(username, password);
    
        QString reply;
    
        QJsonObject userObject;
        QString body = "";
        int bodyLength = 0;
    
        QLocale locale(QLocale::English);
        QDateTime currTime = QDateTime::currentDateTimeUtc();
        QString time = "Date: " + locale.toString(currTime, "ddd, ") + locale.toString(currTime, "d MMM yyyy HH:mm:ss") + " GMT";
    
        if(ok)
        {
            reply = "HTTP/1.1 200 OK\r\n%1\r\nContent-type: application/json\r\nContent-length: %2\r\n\r\n%3";
            userObject["succes"] = "true";
            body = QJsonDocument(userObject).toJson(QJsonDocument::Compact);
        }
        else
        {
            reply = "HTTP/1.1 401 Unauthorized\r\n%1\r\nContent-type: application/json\r\nContent-length: %2\r\n\r\n%3";
            userObject["succes"] = "false";
            body = QJsonDocument(userObject).toJson(QJsonDocument::Compact);
        }
    
        socket->write(reply.arg(time).arg(bodyLength).arg(body).toLocal8Bit());
        socket->flush();
        socket->waitForBytesWritten();
    }
    

  • Lifetime Qt Champion

    From the looks of it, you are expecting that when readyRead is called, all data have arrived which is wrong. You have to first ensure that everything has arrived before parsing it.

    Just in case, there's now a QtHttpServer module that might be of interest for your use case.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.