Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QtWebEngine
  4. QWebEngineHttpRequest - Post request

QWebEngineHttpRequest - Post request

Scheduled Pinned Locked Moved Solved QtWebEngine
2 Posts 1 Posters 1.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Gulivert
    wrote on last edited by Gulivert
    #1

    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.

    1 Reply Last reply
    0
    • G Offline
      G Offline
      Gulivert
      wrote on last edited by
      #2

      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 :-)

      1 Reply Last reply
      3

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved