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. reading from NetworkReply always give incomplete string of data

reading from NetworkReply always give incomplete string of data

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 4 Posters 562 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
    Ragbot
    wrote on last edited by
    #1
    void WebApi::readReady()
    {
        QFile file("response.json");
        file.open(QIODevice::WriteOnly);
        QTextStream stream(&file);
        stream << reply->readAll();
        qDebug() << reply->readAll();
        stream.flush();
    }
    
    

    This code always results in such a response

    // A lot more data here but still cutoff
    -87dd-415e-bbc0-2ec27bf404cc",
            "type": "tag",
            "attributes": {
                "name": {
                    "en": "Fantasy"
                },
                "description": {},
                "group": "genre",
                "version": 1
            },
            "relationships": []
        }
    ],
    "state": "published",
    "chapterNumbersResetOnNewVolume": false,
    "createdAt": "2019-02-09T21: 22: 51+00: 00",
    "updatedAt": "2023-03-10T19: 04: 19+00: 00",
    "version": 8,
    "availableTranslatedLanguages": [
        "en",
        "cs",
        "ru"
    ],
    "latestUploadedChapter": "f729402f-5d8f-4bb4-832a-842e0fa4da9e"
    },
    "relationships": [
    {
        "id": "43fa8935-9025-4f35-afb6-ba64e7883fa2",
        "type": "author"
    },
    {
        "id": "43fa8935-9025-4f35-afb6-ba64e7883fa2",
        "type": "artist"
    },
    {
        "id": "81af78b3-9a6c-4fb9-8add-348b92275b9e",
        "type": "cover_art"
    }
    ]
    }
    ],
    "limit": 10,
    "offset": 0,
    "total": 2
    }
    

    why is it so?
    where am i going wrong?

    JonBJ 1 Reply Last reply
    0
    • R Ragbot
      void WebApi::readReady()
      {
          QFile file("response.json");
          file.open(QIODevice::WriteOnly);
          QTextStream stream(&file);
          stream << reply->readAll();
          qDebug() << reply->readAll();
          stream.flush();
      }
      
      

      This code always results in such a response

      // A lot more data here but still cutoff
      -87dd-415e-bbc0-2ec27bf404cc",
              "type": "tag",
              "attributes": {
                  "name": {
                      "en": "Fantasy"
                  },
                  "description": {},
                  "group": "genre",
                  "version": 1
              },
              "relationships": []
          }
      ],
      "state": "published",
      "chapterNumbersResetOnNewVolume": false,
      "createdAt": "2019-02-09T21: 22: 51+00: 00",
      "updatedAt": "2023-03-10T19: 04: 19+00: 00",
      "version": 8,
      "availableTranslatedLanguages": [
          "en",
          "cs",
          "ru"
      ],
      "latestUploadedChapter": "f729402f-5d8f-4bb4-832a-842e0fa4da9e"
      },
      "relationships": [
      {
          "id": "43fa8935-9025-4f35-afb6-ba64e7883fa2",
          "type": "author"
      },
      {
          "id": "43fa8935-9025-4f35-afb6-ba64e7883fa2",
          "type": "artist"
      },
      {
          "id": "81af78b3-9a6c-4fb9-8add-348b92275b9e",
          "type": "cover_art"
      }
      ]
      }
      ],
      "limit": 10,
      "offset": 0,
      "total": 2
      }
      

      why is it so?
      where am i going wrong?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Ragbot
      This issue is not to do with design, and belongs in the General forum.

      You cannot just call reply->readAll() and expect it to return something at the instant called. You need to attach a slot for when data arrives. And your data may well arrive in multiple "chunks", not all in one call, so the signal/slot may be called multiple times. If readReady() is a slot (who knows?) it needs to allow to be called several times, e.g. it can't just overwrite an output file.

      Also do not call qDebug() << reply->readAll();, that would do a new read, which I doubt you intend.

      R 1 Reply Last reply
      4
      • J.HilkJ J.Hilk moved this topic from Qt Design Studio on
      • JonBJ JonB

        @Ragbot
        This issue is not to do with design, and belongs in the General forum.

        You cannot just call reply->readAll() and expect it to return something at the instant called. You need to attach a slot for when data arrives. And your data may well arrive in multiple "chunks", not all in one call, so the signal/slot may be called multiple times. If readReady() is a slot (who knows?) it needs to allow to be called several times, e.g. it can't just overwrite an output file.

        Also do not call qDebug() << reply->readAll();, that would do a new read, which I doubt you intend.

        R Offline
        R Offline
        Ragbot
        wrote on last edited by
        #3

        @JonB Thanks I didn't mean to publish it to the design section sorry.

        and yes it is connected via

            reply = client.get(QNetworkRequest(QUrl(base_url + page)));
            connect(reply, &QNetworkReply::readyRead, this, &MangaDex::readReady);
        

        and I do understand the chunking concept but I am not sure how to finish writing those chunks so I don't just keep appending to the previous response.

        How do handle this I?
        Is there a way to know when it is finished?
        if so how do I use it in this context?

        jsulmJ 1 Reply Last reply
        0
        • R Ragbot

          @JonB Thanks I didn't mean to publish it to the design section sorry.

          and yes it is connected via

              reply = client.get(QNetworkRequest(QUrl(base_url + page)));
              connect(reply, &QNetworkReply::readyRead, this, &MangaDex::readReady);
          

          and I do understand the chunking concept but I am not sure how to finish writing those chunks so I don't just keep appending to the previous response.

          How do handle this I?
          Is there a way to know when it is finished?
          if so how do I use it in this context?

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Ragbot said in reading from NetworkReply always give incomplete string of data:

          how to finish writing those chunks so I don't just keep appending to the previous response

          You need some kind of protocol, so you know when you received a whole package...

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          R 1 Reply Last reply
          2
          • jsulmJ jsulm

            @Ragbot said in reading from NetworkReply always give incomplete string of data:

            how to finish writing those chunks so I don't just keep appending to the previous response

            You need some kind of protocol, so you know when you received a whole package...

            R Offline
            R Offline
            Ragbot
            wrote on last edited by Ragbot
            #5

            @jsulm What does that mean?
            I don't own the api so I am not sure, does it have something to do with that?

            also can I just connect QNetworkReply::finished to write the data to file while readReady reads to buffer?

            JonBJ B 2 Replies Last reply
            0
            • R Ragbot

              @jsulm What does that mean?
              I don't own the api so I am not sure, does it have something to do with that?

              also can I just connect QNetworkReply::finished to write the data to file while readReady reads to buffer?

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @Ragbot
              Yes, assuming what you are getting is just JSON content, see void QNetworkReply::finished(). You can also do a readAll() there to read and save reply in one call. Presumably Ok unless package data is huge. Try just moving your readReady() to that signal?

              1 Reply Last reply
              1
              • R Ragbot

                @jsulm What does that mean?
                I don't own the api so I am not sure, does it have something to do with that?

                also can I just connect QNetworkReply::finished to write the data to file while readReady reads to buffer?

                B Offline
                B Offline
                Bob64
                wrote on last edited by
                #7
                This post is deleted!
                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