Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt Simple Post Request

Tags:

post

qt

I'm looking to do a very simple POST request to a webpage. The page is in php and will take whatever is posted check it against a database then respond with a key if the item is in the database.

I have not a clue how to use post requests inside Qt or how to get information returned and store it back into a variable within Qt. Any help would be highly appreciated as I am starting from a blank on the Qt side.

I've looked at the other examples:

https://stackoverflow.com/questions/11348359/qt-https-post-request

How can I POST data to a url using QNetworkAccessManager

but I don't see how to store a response from the php script

like image 642
Matt Stokes Avatar asked Nov 09 '12 04:11

Matt Stokes


2 Answers

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.

like image 94
Damien Avatar answered Jan 03 '23 19:01

Damien


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

like image 41
daka Avatar answered Jan 03 '23 21:01

daka