Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QWebEngineHttpRequest - Post request



  • Hi there,

    I tried to create a post request with QWebEngineHttpRequest to load it in QWebEngineView but this is not working as expected unfortunately.

    Here is the goal. I have currently an simple http get request, and I would like to transform it in a post request, the server allow both requests then this should be not a problem of the server but from my request.

    Here is an example. First I build my request to be a get (this works but is unsecured).

    // Create the base URL
    this->apiUrl.setScheme("https");
    this->apiUrl.setHost("www.domainname.com");
    this->apiUrl.setPath("/path/of/the/api/end/point/");
    
    // Create the query
    qurlqr.addQueryItem("Class", "Patient");
    qurlqr.addQueryItem("Method", "CreateOrder");
    qurlqr.addQueryItem("LoginName", login);
    qurlqr.addQueryItem("Password", password);
    ...
    qurlqr.addQueryItem("patCountry", "CH");
    qurlqr.addQueryItem("treatmentCode", "");
    
    this->apiUrl.setQuery(qurlqr);
    
    // Create an engine view widget
    QWebEngineView *webView = new QWebEngineView(this);
     webView->load(this->apiUrl);
    

    This code works and do a GET request. Now I would like to transform this query in a post request inside my QwebEngineView, to do that I wanna create a QWebEngineHttpRequest .

    Then here is the code to transform this :

    // Create the base URL
    this->apiUrl.setScheme("https");
    this->apiUrl.setHost("www.domainname.com");
    this->apiUrl.setPath("/path/of/the/api/end/point/");
    
    QWebEngineHttpRequest request = QWebEngineHttpRequest();
    request.setUrl(this->apiUrl);
    request.setMethod(QWebEngineHttpRequest::Post);
    request.setHeader(QByteArray("Content-Type"), QByteArray("application/x-wwwform-urlencoded"));
    
    // Create the query
    qurlqr.addQueryItem("Class", "Patient");
    qurlqr.addQueryItem("Method", "CreateOrder");
    qurlqr.addQueryItem("LoginName", login);
    qurlqr.addQueryItem("Password", password);
    ...
    qurlqr.addQueryItem("patCountry", "CH");
    qurlqr.addQueryItem("treatmentCode", "");
    
    // Convert Query URL to QByteArray and set post data to request
    QByteArray postData = qurlqr.toString(QUrl::FullyEncoded).toUtf8();
    request.setPostData(postData);
    
    QWebEngineView *webView = new QWebEngineView(this);
    webView->load(request);
    

    But unfortunately this is not working, I think my problem is comming from postData but not sure. Do you have any advice to help me ? Any help will be appreciate.



  • AAAAAA I'm stupid, I did a spelling error !!!!

    request.setHeader(QByteArray("Content-Type"), QByteArray("application/x-wwwform-urlencoded"));
    

    must be

    request.setHeader(QByteArray("Content-Type"), QByteArray("application/x-www-form-urlencoded"));
    

    And it is working :-)


Log in to reply