8

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

Community
  • 1
  • 1
Matt Stokes
  • 4,618
  • 9
  • 33
  • 56

3 Answers3

12

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.

Damien
  • 1,492
  • 10
  • 32
  • 2
    `mgr->post(QNetworkRequest(url), http);` doesn't compile in Qt 5.15 (no matching function) – M.M Sep 07 '20 at 09:02
  • @M.M I don't think OP can write a code that can be valid for every damn Qt version. – Abbas Perçin Jul 25 '21 at 18:35
  • 1
    @AbbasPerçin even though It is usual in StackOverflow to update answers with newer or easier or more elegant solutions, i don't believe M.M is asking for an update but letting others know that they are not alone experiencing this issue. I also could not compile the code. – no more sigsegv Jul 27 '21 at 10:48
  • This function seems to still exist in Qt5.15 -> https://doc.qt.io/qt-5/qnetworkaccessmanager.html#post-2 perhaps you have Qt compiled with some missing flags or is it a bug within Qt? @M.M – Damien Jul 27 '21 at 10:55
  • 1
    @Damien maybe the documentation is out of date? – M.M Jul 27 '21 at 10:57
  • Try mgr->post(QNetworkRequest(url), &http); @M.M – Damien Jul 27 '21 at 10:58
8
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

olfek
  • 3,210
  • 4
  • 33
  • 49
-1

Connect the QNetworkAccessManager signal finished to your slot and using QNetworkReply you should read all the contents of the webpage.

Here is a get example it can be easily adapted for the post method.

void MainWindow::on_pushButton_clicked()
{
    QNetworkAccessManager * mgr = new QNetworkAccessManager(this);
    connect(mgr,SIGNAL(finished(QNetworkReply*)),this,SLOT(onfinish(QNetworkReply*)));
    connect(mgr,SIGNAL(finished(QNetworkReply*)),mgr,SLOT(deleteLater()));

    mgr->get(QNetworkRequest(QUrl("http://www.google.com")));

}

void MainWindow::onfinish(QNetworkReply *rep)
{
    QByteArray bts = rep->readAll();
    QString str(bts);
    QMessageBox::information(this,"sal",str,"ok");

}
opc0de
  • 11,557
  • 14
  • 94
  • 187
  • 11
    This doesn't answer the question on how to do a POST request, post needs data, this is simply a GET request. – Damien Nov 30 '18 at 11:06
  • 1
    Why wouldn't you add the eaxmple of a post? This answer gets shown at duckduckgo, but this isn't a correct answer – Nebulosar May 29 '19 at 11:25