High level wrapper around QNetworkAccessManager for make network communications easy.



  • 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.


  • Moderators

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


  • @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!:)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.