Qt World Summit: Register Today!

Empty buffer when large Json content is downloaded using QNetworkAccessManager object

  • 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);


    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 ?

  • Moderators

    @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

Log in to reply