Important: Please read the Qt Code of Conduct -

Synchronous QT HTTP GET for Json parsing

  • Hi,
    I need my application to communicate with a server that sends data as JSON.
    The data should not exceed a few kb.
    Here is how it works :
    Initial situation : I have a record ID. I want to download this record.
    Step 1 : I ask the server about this record, using something like<my ID>.
    Step 2 : The server display a json page which holds data about this record, containing the download URL of the record
    Step 3 : My app parses the JSON data to find the download Url. After that, my app downloads the file asynchronously.

    I am now currently at step 2.
    I need to HTTP GET this data synchronously and in a blocking way. I dont need to store the record JSON data on disk or anything, I just want to GET it and to parse it in a single function without having to use the SIGNAL/SLOT system.

    All the info I find on the internet about using Qt GET is either asynchronous, or overkill.
    How should I do it ?

  • Lifetime Qt Champion

    @rackover use signals and slots and parse the received data in the slot.

    I hear a lot that people want a blocking solution and most often they can use the non-blocking without problems.

  • I'm in a for loop, as I will need to get multiple records information. If I start using signals and slot, I will need to await them all to be finished, and that will add another layer of complexity over an otherwise very simple need. Also, having to wait for something to complete before executing further code, means that it will be blocking anyway. So there's no point in making it asynchronous at all.

    I need the whole "download URLs fetching" to be held in a single function.

  • Lifetime Qt Champion

    @rackover then you will block Qt's event loop and your program will not work as expected, if at all.

    the only way for blocking operations is in a separate thread.

  • What do you mean "it won't work as expected, if at all" ? And by "blocking operations in a separate thread" ?

    Could you elaborate a bit ?

  • Lifetime Qt Champion


    Qt's application uses an event loop, if you put a while loop in you are application, you'll block the event loop and thus your application.

    Why would it be more complicated to use the asynchronous nature of Qt ? From the looks of it, each step has to wait on the previous one. Number one and two looks like a call to QNetworkAccessManager::get while number three the content of the slot connected to the finished signal.

  • This is a very simple application / tool. It doesn't have GUI at all, it is not supposed to be responsive or fluid by any way. The fact that the application freezes or hangs during download isn't a big deal at all.

    If I use the finished signal, will I still be able to make the whole 1) 2) + json parsing operations in a single function ?

  • Lifetime Qt Champion

    With a lambda you can keep everything pretty tidy.

Log in to reply