Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Need help with get request and redirections



  • Hello everyone.
    I am very new to QT or coding in general so be gentle :)

    I am looking at downloading some csv data from a google spreadsheet.

    I have a script with a get function that retrun the csv data.
    It works correctly on my navigator.

    Now, i have tried to download datas with the following code :

    QNetworkAccessManager man;
        QNetworkRequest req(QUrl("https://script.google.com/macros/s/AKfycbySdq6clPuvG06h1bLgy1Ag4eOeutDvT0084efJpCf9Niqw-Pc/exec"));
        QNetworkReply*reply=man.get(req);
        QObject::connect(reply, &QNetworkReply::finished,[&](){
    
            QByteArray read = reply->readAll();
            //QFile req("test.csv");
            QFile out("csvdata.csv");
            out.open(QIODevice::WriteOnly|QIODevice::Text);
            out.write(read);
            out.close();
            reply->close();
            reply->deleteLater();
            a.quit();
    

    but the result is always a html error of some kind :
    <HTML>
    <HEAD>
    <TITLE>Moved Temporarily</TITLE>
    </HEAD>
    etc...

    (the error varies but always is the same idea : the link has moved somewhere else)

    So do i have a way to "track" the link or is there something i have to do to prevent that to happend ?
    I thought it would be simple... basic script that return text data, basic get request and basic file writting...
    ...
    been on that for the last 6hours...

    Knowing that the code works great if i point to a text file link somewhere.
    So i guess the fault is with google and url processing

    Im tired, please. Help me :)



  • A simple approach is to add

    req.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy);
    

    But this will not allow "https" to "http".



  • A simple approach is to add

    req.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy);
    

    But this will not allow "https" to "http".



  • @Bonnie said in Need help with get request and redirections:

    req.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy);

    Wouhouuuu, you save my week i guess :)
    Thank you so much, that worked like a charm.


Log in to reply