Answer the question
In order to leave comments, you need to log in
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:
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);
}
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);
}
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question