c++qtpostqnetworkaccessmanagerqnetworkreply

QNetworkRequest returning null


I have to use post to a google script (see below) and I want in the end of the script receive a feedback from it that everything went okay.

function doPost(e){
   var idm = e.parameter.idm;
   var sheet_purchaseHistory = SpreadsheetApp.getActive().getSheetByName('purchaseHistory');
   var date = new Date();
   var time = Utilities.formatDate(date, 'Asia/Tokyo', 'yyyy/MM/dd HH:mm:ss');
   sheet_purchaseHistory.getRange(sheet_purchaseHistory.getLastRow()+1, 1).setValue(time);
   sheet_purchaseHistory.getRange(sheet_purchaseHistory.getLastRow(), 2).setValue(mailadress);
   return ContentService.createTextOutput(JSON.stringify({'status': 'success'})).setMimeType(ContentService.MimeType.JSON);
}

In my cpp code I have:

header (.h)

QNetworkRequest request;
QNetworkAccessManager *manager;
QByteArray ba;
QNetworkReply* reply;

Constructor:

manager = new QNetworkAccessManager(this);  // I tried with and without parsing the (this)
request.setUrl(QUrl("https://script.google.com/macros/s/AKfycbzhHBpZ8jMfa2bwE_A0lQJNqOzVl894dcjkch_-0mNC6EX9usn1/exec"));

Inside a function:

manager->setRedirectPolicy(QNetworkRequest::NoLessSafeRedirectPolicy);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
QUrlQuery postData;
postData.addQueryItem("idm", "string");
QNetworkReply *rp = manager->post(request,postData.toString(QUrl::FullyEncoded).toUtf8());
QEventLoop loop;
connect(rp, SIGNAL(finished()), &loop, SLOT(quit())); //readready SIGNAL also tried
loop.exec();
QByteArray data = rp->readAll();
QString dataReply(data);
qDebug() << dataReply;

What happens is that I always receive null. I already tried several combinations such as different signals (readyread, finished...), using QUrlQuery and ByteArray, setting different headers, but with no success.

I also noticed that, when my parameters are in my request (https://address?parameters=string) I can receive the return. (In this case by removing the parameters from "postdate" and including them in post "req"

QNetworkReply *rep = man->post(req,postdata);

PRINT OF THE RESTEStTEST: I tried to post using https://resttesttest.com/ and it worked properly. It gave the following comments

Could someone help me please?


Solution

  • With the help of the following command using curl I was able to analyze the request:

    curl  --trace-ascii  output.txt \
          -d "idm=0123" \
          -L "https://script.google.com/macros/s/AKfycbzhHBpZ8jMfa2bwE_A0lQJNqOzVl894dcjkch_-0mNC6EX9usn1/exec" 
    

    In the .txt file is the following line:

    # ...
    == Info: Issue another request to this URL: 'https://script.googleusercontent.com/macros/echo?user_content_key=odFvp9PfkjN-H6-IhBUWhj1XC4LbgGXq3moew78NtUkeQAI4UCwjEaz33jH1p_VSrMLvl9R-1p2f-LCuIURpcA9ynEuB76Kdm5_BxDlH2jW0nuo2oDemN9CCS2h10ox_1xSncGQajx_ryfhECjZEnDmqvNuuY9_pIuNLYjrNDt9AJiVy2Q9_jt9ucsQKYIYmzKj2_WuA-BZmfTxlnQZT_cbqvF_hZXXo&lib=MbJJIMSCHwBgrN--y_1G9PbYl-ov70HvX'
    == Info: Switch from POST to GET
    # ...
    

    That tells us that in the redirection is changed from POST to GET so it is not taken into account with the attribute QNetworkRequest::NoLessSafeRedirectPolicy that continues trying with POST. So the solution is to handle the redirect manually.

    QNetworkAccessManager manager;
    
    QNetworkRequest request;
    QUrl url("https://script.google.com/macros/s/AKfycbzhHBpZ8jMfa2bwE_A0lQJNqOzVl894dcjkch_-0mNC6EX9usn1/exec");
    request.setUrl(url);
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
    QUrlQuery postData;
    postData.addQueryItem("idm", "string");
    QNetworkReply *reply_post = manager.post(request,  postData.toString(QUrl::FullyEncoded).toUtf8());
    QEventLoop loop;
    QObject::connect(reply_post, &QNetworkReply::finished, &loop, &QEventLoop::quit);
    loop.exec();
    
    QNetworkRequest redirect_request;
    redirect_request.setUrl(reply_post->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl());
    reply_post->deleteLater();
    QNetworkReply *reply_get = manager.get(redirect_request);
    QObject::connect(reply_get, &QNetworkReply::finished, &loop, &QEventLoop::quit);
    loop.exec();
    qDebug()<< reply_get->readAll();
    reply_get->deleteLater();
    

    Output:

    OK