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. Empty buffer when large Json content is downloaded using QNetworkAccessManager object
Forum Updated to NodeBB v4.3 + New Features

Empty buffer when large Json content is downloaded using QNetworkAccessManager object

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 233 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.
  • U Offline
    U Offline
    Uday More
    wrote on last edited by
    #1

    I am downloading a json file using an URL and the QNetworkAccessManager class. For smaller files till ~50 MB, I am able to successfully download the files. But when the size increases further, I get a success code as success (200) and when I try to access the Bytearray to from the json content, the bytearray returned is empty.

    Code :
    QString url = "My URL to fetch content";
    QNetworkRequest requestGetLabels(QUrl::fromEncoded(url.toLocal8Bit()));
    requestGetLabels.setRawHeader("Authorization", m_linkCredentialsBytes);

    // Setting the header type requestGetLabels.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
    QNetworkAccessManager *restApiClient = new QNetworkAccessManager();
    QNetworkReply *replyGetLabels = restApiClient->get(requestGetLabels);

    while(!replyGetLabels->isFinished())
    {
    qApp->processEvents();
    }

    QVariant statusCode = replyGetLabels->attribute(QNetworkRequest::HttpStatusCodeAttribute);
    LMSError replyStatus = static_cast<LMSError>(statusCode.toInt());

    // Check if the API call is successful.
    if(LMSError::SUCCESS == replyStatus) ==> 200 is LMSError::SUCESS
    {
    //==> It returns zero bytes here for files
    QByteArray bytes = replyGetLabels->readAll();
    }

    Also some questions :

    1. What is the default timeout in seconds for get and a post request ?
    J.HilkJ 1 Reply Last reply
    0
    • U Uday More

      I am downloading a json file using an URL and the QNetworkAccessManager class. For smaller files till ~50 MB, I am able to successfully download the files. But when the size increases further, I get a success code as success (200) and when I try to access the Bytearray to from the json content, the bytearray returned is empty.

      Code :
      QString url = "My URL to fetch content";
      QNetworkRequest requestGetLabels(QUrl::fromEncoded(url.toLocal8Bit()));
      requestGetLabels.setRawHeader("Authorization", m_linkCredentialsBytes);

      // Setting the header type requestGetLabels.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
      QNetworkAccessManager *restApiClient = new QNetworkAccessManager();
      QNetworkReply *replyGetLabels = restApiClient->get(requestGetLabels);

      while(!replyGetLabels->isFinished())
      {
      qApp->processEvents();
      }

      QVariant statusCode = replyGetLabels->attribute(QNetworkRequest::HttpStatusCodeAttribute);
      LMSError replyStatus = static_cast<LMSError>(statusCode.toInt());

      // Check if the API call is successful.
      if(LMSError::SUCCESS == replyStatus) ==> 200 is LMSError::SUCESS
      {
      //==> It returns zero bytes here for files
      QByteArray bytes = replyGetLabels->readAll();
      }

      Also some questions :

      1. What is the default timeout in seconds for get and a post request ?
      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @Uday-More you're forcing synchronous behavior on an asynchronous API, thats your problem.

      Change it to proper event driven code and also connect to the various error signals of QNetworkAccessManager and QNetworkReply


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      3

      • Login

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