Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Showcase
  4. High level wrapper around QNetworkAccessManager for make network communications easy.

High level wrapper around QNetworkAccessManager for make network communications easy.

Scheduled Pinned Locked Moved Unsolved Showcase
3 Posts 2 Posters 1.9k Views 2 Watching
  • 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.
  • D Offline
    D Offline
    DOGG_11
    wrote on last edited by DOGG_11
    #1

    Hi guys! We publish our small library for simplify interacting with web for Qt.

    For example you can load web-page like this:

    const QByteArray data = NetworkRequestLoader::loadSync("https://github.com");
    

    Or like this

    NetworkRequestLoader::loadAsync("https://github.com", [] (const QByteArray& _loadedData) {
        qDebug() << "Loaded" << _loadedData.size() << "bytes.";
    });
    

    Want to send POST request? It's easy!

    NetworkRequest request;
    requset.setRequestMethod(NetworkRequest::Post);
    request.addRequestAttribute("id", 1893);
    request.addRequestAttributeFile("photo", "/home/user/Images/photo.png");
    const QByteArray postStatus = request.loadSync("https://site.com/API/v1/savePhoto/");
    

    Github link https://github.com/dimkanovikov/WebLoader

    We hope, it will be useful. If you have some questions, we ready to give answers for you.

    1 Reply Last reply
    1
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi, thanks for sharing.

      I have a question and a suggestion.

      The question is - does this preserve sessions? If I make a login request and then some resource request for that session will it work? I guess it's really a question of do you use a single QNetworkAccessManager for all requests or change them underneath. If one then it would be nice to get access to it somehow or set my own if I already have one for other purposes.

      There's also this example:

      foreach (const QString& url, thousandsUrls) {
          NetworkRequestLoader::loadAsync(url, [] (const QByteArray& _loadedData, const QUrl& _fromUrl) {
              qDebug() << "Loaded" << _loadedData.size() << "bytes from" << _fromUrl;
          });
      }
      

      This has a downside that it creates thousands of instances of that lambda object. If the lambda used some heavy state capture this can become a problem. Of course I could do

      auto someLambda = [] (const QByteArray& _loadedData, const QUrl& _fromUrl) {
         qDebug() << "Loaded" << _loadedData.size() << "bytes from" << _fromUrl;
      }
      foreach (const QString& url, thousandsUrls) {
          NetworkRequestLoader::loadAsync(url, someLambda);
      }
      

      but it's not as pretty and it could be usefull to add an overload that would simply let me pass a container of urls and do the loop for me:

      NetworkRequestLoader::loadAsync(thousandsUrls, [] (const QByteArray& _loadedData, const QUrl& _fromUrl) {
              qDebug() << "Loaded" << _loadedData.size() << "bytes from" << _fromUrl;
      });
      
      D 1 Reply Last reply
      2
      • Chris KawaC Chris Kawa

        Hi, thanks for sharing.

        I have a question and a suggestion.

        The question is - does this preserve sessions? If I make a login request and then some resource request for that session will it work? I guess it's really a question of do you use a single QNetworkAccessManager for all requests or change them underneath. If one then it would be nice to get access to it somehow or set my own if I already have one for other purposes.

        There's also this example:

        foreach (const QString& url, thousandsUrls) {
            NetworkRequestLoader::loadAsync(url, [] (const QByteArray& _loadedData, const QUrl& _fromUrl) {
                qDebug() << "Loaded" << _loadedData.size() << "bytes from" << _fromUrl;
            });
        }
        

        This has a downside that it creates thousands of instances of that lambda object. If the lambda used some heavy state capture this can become a problem. Of course I could do

        auto someLambda = [] (const QByteArray& _loadedData, const QUrl& _fromUrl) {
           qDebug() << "Loaded" << _loadedData.size() << "bytes from" << _fromUrl;
        }
        foreach (const QString& url, thousandsUrls) {
            NetworkRequestLoader::loadAsync(url, someLambda);
        }
        

        but it's not as pretty and it could be usefull to add an overload that would simply let me pass a container of urls and do the loop for me:

        NetworkRequestLoader::loadAsync(thousandsUrls, [] (const QByteArray& _loadedData, const QUrl& _fromUrl) {
                qDebug() << "Loaded" << _loadedData.size() << "bytes from" << _fromUrl;
        });
        
        D Offline
        D Offline
        DOGG_11
        wrote on last edited by
        #3

        @Chris-Kawa big thanks for you answer and suggestion!

        For save session across requests you can use instance of QNetworkCookieJar class, like this:

        QNetworkCookieJar cookies;
        NetworkRequest request;
        request.setCookieJar(&cookies);
        request.loadSync("https://site.com/API/v1/startSession");
        
        // And in next step you can reuse this request or create new NetworkRequest and sent cookies to them
        
        request.loadSync("https://site.com/API/v1/uploadImage");
        

        Thanks for suggestion again. We added patch which allow you to use overloaded method and pass a container of links to load!:)

        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