Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. POST using Qt5
Qt 6.11 is out! See what's new in the release blog

POST using Qt5

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 3.2k Views 1 Watching
  • 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.
  • R Offline
    R Offline
    rosetter
    wrote on last edited by
    #1

    I'm stumbling my way through communicating with a web service and failing badly.. I'm trying to mimic a form that POSTs a chunk of json data to the server in a "json" field. There are similar questions here on Qt Project and elsewhere, but most are using the 4.x APIs which don't translate directly. Help!

    Here's what I'm trying (simplified a little):

    @
    QUrl url("http://.....");
    QUrlQuery query;
    query.addQueryItem("json", <json text as QString>);
    url.setQuery(query);
    QNetworkRequest request(url);
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
    QNetworkAccessManager net(this);
    net.get(request);
    @

    It seems strange to be using "get" to POST data... also the docs suggest that addQueryItem() does not do what a browser would do with a form (converting spaces to pluses) but doesn't give any advice on whether that is important or not.

    Any help would be most appreciated!

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      [quote author="rosetter" date="1389792547"]
      It seems strange to be using "get" to POST data...
      [/quote]
      Indeed, where to you got this example from?!

      @
      QByteArray postData;
      postData.append("json=<json-data>&");
      postData.append("other=<other-data>");

      QNetworkRequest request( QUrl("...") );
              request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
      
      QNetworkAccessManager* net = ...;
      QNetworkReply* reply = net->post(request, postData);
      connect(reply, SIGNAL(...), ..., SLOT(...);
      

      @
      Make sure you use a QNetworkAccessManager by pointer value, or as class member. Just not in a stack variable with function scope...

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • R Offline
        R Offline
        rosetter
        wrote on last edited by
        #3

        Hi, thanks for the quick reply! Unfortunately I'm still super-confused..

        • do I need to do some kind of encoding of postData? (% encoding? space -> '+'?)
        • what about QUrlQuery? is it not intended to be helpful in POSTs?
        • is there some way I can see what is actually being sent over the network?
        1 Reply Last reply
        0
        • raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          [quote author="rosetter" date="1389970769"]

          • do I need to do some kind of encoding of postData? (% encoding? space -> '+'?)
          • what about QUrlQuery? is it not intended to be helpful in POSTs?
            [/quote]
            yes you would need to do the encoding yourself. But still you can use QUrlQuery::query().

          [quote author="rosetter" date="1389970769"]

          • is there some way I can see what is actually being sent over the network?
            [/quote]
            Not on Qt level. You can use a proxy/sniffer application like "wireshark":http://www.wireshark.org.

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          0

          • Login

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