[Interest] QNetwork classes for submitting google forms

Max Paperno max-l at wdg.us
Fri Jun 11 23:10:57 CEST 2021


Hello Nicholas,

> I would like to know if I am using the Qt QNetwork classes correctly
> to submit a post to a URL that is a Google form

 From the docs[1]: QNetworkAccessManager has an asynchronous API.

That means you have to wait for a finished() signal before trying to 
determine the status or read response data. The call to post() returns 
right away, probably before the request is even sent.

The docs have a pretty basic example, but to put it into your context, 
it would look something like this:

// ...
QNetworkAccessManager networkManager;
bool gotResponse = false;

QObject::connect(&networkManager, &networkManager::finished, 
[&gotResponse](QNetworkReply *reply) {
   int status = reply->attribute(
       QNetworkRequest::HttpStatusCodeAttribute).toInt();
   qDebug() << status;
   //  etc....
   gotResponse = true;
});

networkManager.post(request, 
postData.toString(QUrl::FullyEncoded).toUtf8());

while (!gotResponse)
   sleep(1)  // or whatever sleep method, just waiting for a response.

return 0;
}


HTH,
-Max


[1]: https://doc.qt.io/qt-5/qnetworkaccessmanager.html


On 6/11/2021 4:22 PM, Nicholas Yue wrote:
> Hi,
> 
> I am trying to submit google forms programmatically via C++ and Qt classes
> 
> I am basing my study on the following
> 
> https://stackoverflow.com/questions/17964429/google-forms-response-with-python#17965510
> 
> I got the Python version running and returned a 200 response.
> 
> I tried converting the code to C++ and Qt but was not successful. I get 
> a response of '0' zero
> 
> I would like to know if I am using the Qt QNetwork classes correctly to 
> submit a post to a URL that is a Google form
> 
> =============================================================
> #include <QtNetwork/QNetworkRequest>
> #include <QtNetwork/QNetworkAccessManager>
> #include <QUrlQuery>
> #include <QDebug>
> #include <QNetworkReply>
> 
> int main()
> {
> 
>      QUrlQuery postData;
>      postData.addQueryItem("emailAddress", "abc.xyz at gmail.com 
> <mailto:abc.xyz at gmail.com>");
>      postData.addQueryItem("entry.2020959411", "Qt Query");
> 
>      /*
>       *
>      url = 
> 'https://docs.google.com/forms/d/152CTd4VY9pRvLfeACOf6SmmtFAp1CL750Sx72Rh6HJ8/formResponse'
>      form_data = {'entry.2020959411': '18+ sollte absolute Pflicht sein',
>                   #'entry.2020959411': 'Alter sollte garkeine Rolle 
> spielen',
>                   #'entry.2020959411': '17+ wäre für mich vertretbar',
>                   #'entry.2020959411': '16+ wäre für mich vertretbar',
>                   #'entry.2020959411': '15+ wäre für mich vertretbar',
>                   #'entry.2020959411': 'Ausnahmen von der Regel - Dafür?',
>                   #'entry.2020959411': 'Ausnahmen von der Regel - Dagegen?',
>                   #'entry.2020959411': '__other_option__',
>                   #'entry.2020959411.other_option_response': 'test',
>                   'draftResponse': [],
>                   'pageHistory': 0}     *
>       */
> 
>      QUrl 
> serviceUrl("https://docs.google.com/forms/d/152CTd4VY9pRvLfeACOf6SmmtFAp1CL750Sx72Rh6HJ8/formResponse");
> 
>      // ...
>      QNetworkRequest request(serviceUrl);
>      //request.setHeader(QNetworkRequest::ContentTypeHeader,
>      //    "application/x-www-form-urlencoded");
>      QNetworkAccessManager networkManager;
>      QNetworkReply* reply;
>      reply = networkManager.post(request, 
> postData.toString(QUrl::FullyEncoded).toUtf8());
>      QVariant statusCode = reply->attribute( 
> QNetworkRequest::HttpStatusCodeAttribute );
>      int status = statusCode.toInt();
>      qDebug() << status;
>      if ( status != 200 )
>      {
>          QString reason = reply->attribute( 
> QNetworkRequest::HttpReasonPhraseAttribute ).toString();
>          qDebug() << reason;
>      }
> 
> return 0;
> }
> 
> -- 
> Nicholas Yue
> Graphics - Arnold, Alembic, RenderMan, OpenGL, HDF5
> Custom Dev - C++ porting, OSX, Linux, Windows
> http://au.linkedin.com/in/nicholasyue
> https://vimeo.com/channels/naiadtools
> 
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> https://lists.qt-project.org/listinfo/interest
> 



More information about the Interest mailing list