How can I POST data to a url using QNetworkAccessManager

Web ServicesQtPostSymbian

Web Services Problem Overview


I have a webservice that I need to POST some data to using Qt. I figured that I can use a QByteArray when POSTing to the web service.

My question is, how can I format this array in order to be parsed correctly at the other end?

This is the code I have so far:

    // Setup the webservice url
    QUrl serviceUrl = QUrl("http://myserver/myservice.asmx");
    QByteArray postData;

   /* 
   Setup the post data somehow
   I want to transmit:
   param1=string,
   param2=string
   */
       
    // Call the webservice
    QNetworkAccessManager *networkManager = new QNetworkAccessManager(this);
    connect(networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(serviceRequestFinished(QNetworkReply*)));
    networkManager->post(QNetworkRequest(serviceUrl), postData);

Thanks!

Web Services Solutions


Solution 1 - Web Services

Since some parameters and values might need to be utf-8 and percent encoded (spaces, &, =, special chars...), you should rather use QUrl (for Qt 4) or QUrlQuery (for Qt 5) to build the posted string.

Example code for Qt 4:

QUrl postData;
postData.addQueryItem("param1", "string");
postData.addQueryItem("param2", "string");
...
QNetworkRequest request(serviceUrl);    
request.setHeader(QNetworkRequest::ContentTypeHeader, 
    "application/x-www-form-urlencoded");
networkManager->post(request, postData.encodedQuery());

and for Qt 5:

QUrlQuery postData;
postData.addQueryItem("param1", "string");
postData.addQueryItem("param2", "string");
...
QNetworkRequest request(serviceUrl);
request.setHeader(QNetworkRequest::ContentTypeHeader, 
    "application/x-www-form-urlencoded");
networkManager->post(request, postData.toString(QUrl::FullyEncoded).toUtf8());

Starting with Qt 4.8 you can also use QHttpMultiPart if you need to upload files.

Solution 2 - Web Services

I used:

QByteArray postData;
postData.append("param1=string&");
postData.append("param2=string");

So & instead of newline after each parameter.

Solution 3 - Web Services

Updating alexisdm answer to Qt5:

// Setup the webservice url
QUrl serviceUrl = QUrl("http://your.url");
QByteArray postData;

QUrlQuery query;
query.addQueryItem("param1","string1");
query.addQueryItem("param2","string2");

postData = query.toString(QUrl::FullyEncoded).toUtf8();

// Call the webservice
QNetworkAccessManager *networkManager = new QNetworkAccessManager(this);
connect(networkManager, SIGNAL(finished(QNetworkReply*)),
        SLOT(onPostAnswer(QNetworkReply*)));

QNetworkRequest networkRequest(serviceUrl);
networkRequest.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded");

networkManager->post(networkRequest,postData);

Don't forget to include

QT += network

in .pro.

Solution 4 - Web Services

the actually answer is

QByteArray postData;
postData.append("param1=string&");
postData.append("param2=string");

NOTE: use "&" here!!!.

I didn't notice Juha's answer here, and waste much time on testing my code using the ",\n" approach.

Please change the correct answer to Juha's.

Solution 5 - Web Services

Here is another way to handle this, i am using your code also to give a complete code:

     // Setup the webservice url         
     QUrl serviceUrl = QUrl("http://myserver/myservice.asmx");
     QByteArray postData;

     QUrl params;
     params.addQueryItem("param1","string1");
     params.addQueryItem("param2","string2");

     postData = params.encodedQuery();

     // Call the webservice
     QNetworkAccessManager *networkManager = new QNetworkAccessManager(this);
     connect(networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(serviceRequestFinished(QNetworkReply*)));
    
      networkManager->post(QNetworkRequest(serviceUrl), postData);

Solution 6 - Web Services

QByteArray postData;
postData.append("param1=string,\n");
postData.append("param2=string\n");

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsabbourView Question on Stackoverflow
Solution 1 - Web ServicesalexisdmView Answer on Stackoverflow
Solution 2 - Web ServicesJuha PalomäkiView Answer on Stackoverflow
Solution 3 - Web ServicesJonaiasView Answer on Stackoverflow
Solution 4 - Web ServicesMason ZhangView Answer on Stackoverflow
Solution 5 - Web ServicesMugunthView Answer on Stackoverflow
Solution 6 - Web ServicesguruzView Answer on Stackoverflow