QNetworkRequest- Putting QByteArray data?



  • I am trying to upload an XML file to my database using QNetworkAccessManager.
    Here is an example of a XML file I'm trying to upload.

    I can upload fine the text file as a standard text (not compressed), but if I compress the file, then try to upload it, it does not seem to work using QUrlQuery.

    The XML file is just program settings that are saved to the database, so that user can keep their settings on any computer. I do not want to create fields in the DB for each settings, so I figured uploading the XML file would be easy.

    The XML file is 4kb, and the compressed file is 1kb. Not sure compressing it is worth it, but it will speed up the retrieval from the database if I get it to work I guess.

    Here is my full code:

     QNetworkReply* UserDAO::putAccount(Account *account) {
    
    
        QNetworkAccessManager *managerWS = qApp->property("NetworkManagerWS").value<QNetworkAccessManager*>();
        Settings *mySettings = qApp->property("User_Settings").value<Settings*>();
    
        const QString url =  Environnement::getURLEnvironnementWS() + "api/account_rest/account/";
        QUrlQuery postData;
    
        postData.addQueryItem("id", QString::number(account->id) );
        postData.addQueryItem("session_mt_id", account->session_mt_id);
        postData.addQueryItem("last_lang",  mySettings->language);
        postData.addQueryItem("last_os", account->os);
        postData.addQueryItem("FTP", QString::number(account->FTP) );
        postData.addQueryItem("LTHR", QString::number(account->LTHR) );
        postData.addQueryItem("minutes_rode", QString::number(account->minutes_rode) );
        postData.addQueryItem("weight_kg", QString::number(account->weight_kg) );
        postData.addQueryItem("weight_in_kg", QString::number(account->weight_in_kg) );
        postData.addQueryItem("height_cm", QString::number(account->height_cm) );
        postData.addQueryItem("trainer_curve_id", QString::number( account->powerCurve.getId()) ) ;
        postData.addQueryItem("wheel_circ", QString::number(account->wheel_circ) );
        postData.addQueryItem("bike_weight_kg", QString::number(account->bike_weight_kg) );
        postData.addQueryItem("bike_type", QString::number(account->bike_type) );
    
        // Upload settings file to Database, database field is a "mediumtext utf8_general_ci" in MYSQL
        // Work if I use the uncompressed version of the file, but not the .zip
        QString nameFileZip = Util::getMaximumTrainerDocumentPath() + QDir::separator() + account->email_clean + ".zip";
        QFile infile(nameFileZip);
        infile.open(QIODevice::ReadOnly);
        QByteArray compressedData = infile.readAll();
        qDebug() << "uncompressData size" << compressedData.size() << "text is" << QString(compressedData);
        postData.addQueryItem("settings_file", compressedData );
    
        QNetworkRequest request;
        request.setUrl(url);
        request.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded");
    
        QNetworkReply *replyPutUser = managerWS->put(request, postData.toString(QUrl::FullyEncoded).toUtf8() );
    
        qDebug() << "putAccount end";
        return replyPutUser;
    }


  • On second thought, I'm thinking of creating a new table "Setting" instead of uploading an XML.. may be cleaner and more reliable


  • Lifetime Qt Champion

    Hi,

    What's on the other end ? A REST service ?



  • Hey @SGaist,

    On the other end, it's my own REST service (made with php/codeIgniter) and it's a bit limited..
    Here is what I did and it works so far:

    • Created a field in my DB for each settings with default values (default settings values)
    • I pass all the data in a put request. (post not working with codeIgniter for some reason...)

    See code
    Note, I'm using "underscore-case" variable naming to match the DB fields name, not using any ORM so it saves time.

    It's working, but I'm not sure if putting that much data can be a problem later on if I need to add more fields?
    Thanks


  • Lifetime Qt Champion

    Are you writing that service by hand or use a REST library for that ?

    You should only send a JSON document when calling your API not everything in the query parameters



  • This is the REST library used

    Not sure what you mean by "You should only send a JSON document when calling your API not everything in the query parameters"

    I send this JSON document at the end of the program execution, so that it saves the data on the server. I don't check for each variable individually (if they changed or not) So I just pass all the data at once. Code updated on git ;)

    bonne fin de semaine!


Log in to reply
 

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