0

I'm porting an internal browser from QtWebKit to QtWebEngine.

I want a function to request url while posting some data. With WebKit, I could use the following:

With class WebView derived from QtWebView :

void WebView::loadPostUrl(const QUrl &url, QByteArray postdata)
{
    m_initialUrl = url;
    QNetworkRequest request = QNetworkRequest(url);
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
    load(request, QNetworkAccessManager::PostOperation, postdata);
}

Since QtWebEngine does interact with QNetworkAccessManager how can we have the same functionalities with QtWebEngine ?

Thanks

ruddy
  • 135
  • 4
  • 12
  • You could have it load a webpage which then in turn performs the POST request. – MrEricSir Jan 30 '16 at 19:32
  • My QT application have the values of the parameters to post to that URL. So you want me to call another url with those parameters in GET which will then call the main URL with POST ? – ruddy Jan 30 '16 at 23:21
  • That's one method. Another would be to send the data to the page via WebSockets. – MrEricSir Jan 30 '16 at 23:41
  • All those methods require access and modifications to the server side which I don't want to do. – ruddy Jan 31 '16 at 03:23
  • Any news on POSTing using QWebEngine? Me has the same problem to port a QWebView::load() call to QWebEngine ... – Heiko Nardmann May 11 '16 at 15:20
  • I also have the same problem. load() only takes 1 argument. This seems like a huge oversight if we cannot POST – Abei Villafane May 16 '16 at 22:58

1 Answers1

2

My solution was using QWebEnginePage::runJavaScript() to script the login instead of simulating the Post Operation.

QString strLoginScript(
    "var formElts = document.getElementById('formSignIn').elements;"
    "formElts['inputLoginName'].value = '%1';"
    "formElts['inputPassword'].value = '%2';"
    "formElts['btnSignIn'].click();")
    .arg(strUsername)
    .arg(strPassword);

// execute JavaScript code on current page
webEngineView->page()->runJavaScript(strLoginScript);

The optional last parameter (not shown here) is a lambda function that executes when your JavaScript function exits and receives the last value output.

The documentation is not clear on the subject, but I think the function executes asynchronously from the main thread - otherwise you would just block execution and wait, instead of passing in an optional lambda/functor/function-pointer to execute later.