Qt Simple Post Request

opc0de previous answer is not a POST to me but a GET.

Here is how to do a POST Request

void xxx::postRequest(QByteArray & postData)
{
    QUrl url = QUrl("abc.com");


    QNetworkAccessManager * mgr = new QNetworkAccessManager(this);

    connect(mgr,SIGNAL(finished(QNetworkReply*)),this,SLOT(onFinish(QNetworkReply*)));
    connect(mgr,SIGNAL(finished(QNetworkReply*)),mgr,SLOT(deleteLater()));

    QHttpMultiPart http;

    QHttpPart receiptPart;
    receiptPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"data\""));
    receiptPart.setBody(postData);

    http.append(receiptPart);

    mgr->post(QNetworkRequest(url), http);
}

void xxx::onFinish(QNetworkReply *rep)
{

}

from the doc here.


QNetworkAccessManager * manager = new QNetworkAccessManager(this);

QUrl url("https://accounts.google.com/o/oauth2/token");
QNetworkRequest request(url);

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

QUrlQuery params;
params.addQueryItem("client_id", "...");
params.addQueryItem("client_secret", "...");
params.addQueryItem("code", "...");
// etc

connect(manager, SIGNAL(finished(QNetworkReply *)), this, SLOT(replyFinished(QNetworkReply *)));

manager->post(request, params.query().toUtf8());

source

Tags:

Qt

Post