A
A
AlphaScript2018-11-22 21:50:39
Qt
AlphaScript, 2018-11-22 21:50:39

How to correctly insert data into the form and send (on the site)?

Good day to all. I have a site address, and id fields and form buttons. Please tell me how to insert data into the fields and click on the button. I understand that you need to implement using a POST request. I have a code but it doesn't work:

CODE
void MainWindow::requestApi(QByteArray & login, QByteArray & pass)
{

    QNetworkRequest request(QUrl("http://site.net/index.php"));
    QByteArray data;
    data.append("Content-Disposition: form-data; id=\"username\"\r\n\r\n");
    data.append(login + "\r\n");
    data.append("Content-Disposition: form-data; id=\"password\"\r\n\r\n");
    data.append(pass + "\r\n");
    QNetworkAccessManager * pManager = new QNetworkAccessManager();
    QNetworkReply * reply = pManager->post(request, data);
}

And I also tried this:
void MainWindow::requestApi(QByteArray & login, QByteArray & pass)
{

    QNetworkRequest request(QUrl("http://site.net/index.php"));
    QByteArray data;

    data.append("id=\"username\"; value=\"" + login + "\"\r\n");
    data.append("id=\"password\"; value=\"" + pass + "\"\r\n");

    QNetworkAccessManager * pManager = new QNetworkAccessManager();
    QNetworkReply * reply = pManager->post(request, data);
}

p.s. Последний способ - чисто методом "тыка"

PS Relatively recently I switched to Qt, so I ask you not to criticize me too much.
Thank you in advance for your answers

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
SerJook, 2018-11-24
@SerJook

You are trying to send a request in the wrong format.
You most likely need the application/x-www-form-urlencoded format.
It is necessary to use not id fields, but their names (name). It is also possible to include a submit button in the request if it has a name. Often, on the server side, they check for the presence of a button in a request.

QNetworkRequest request(QUrl("http://site.net/index.php"));
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");

QUrlQuery postData;
postData.addQueryItem("username", login);
postData.addQueryItem("password", pass);
postData.addQueryItem("submit_button", "Submit!");

QNetworkAccessManager * pManager = new QNetworkAccessManager(this);
QNetworkReply * reply = pManager->post(request, postData.toString(QUrl::FullyEncoded).toUtf8());

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question