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. QNetworkRequest + How to add a json text in a PUT REST ?
Forum Updated to NodeBB v4.3 + New Features

QNetworkRequest + How to add a json text in a PUT REST ?

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 3.7k 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.
  • M Offline
    M Offline
    mugunth
    wrote on last edited by
    #1

    I am trying to use PUT request using QNetworkAccessManager. I need to know how to add body text to the PUT request.

    Example of my PUT request url will be like this: example.com/member/14 (14 is member id to udpate)
    I need to send the following body json string to update age of the member: { "age": 35} I could not find relevant qt documentation on how to pass the body text in REST requests.
    Need some help on this. Thanks.

    1 Reply Last reply
    0
    • H Offline
      H Offline
      Huulivoide
      wrote on last edited by
      #2

      I haven't used the Qt's network stack that much, but what I read from the documentation something like the following should do it.

      QString jsondata("{'age': 35}");
      QNetworkAccessmanager mngr;
      
      QNetworkRequest request("http://my.org/memebr/14");
      request.setHeader(QNetworkRequest::ContentTypeHeader, QString("application/json"));
      
      QNetworkReply* reply = mngr.put(request, jsondata.toUtf8());
      
      1 Reply Last reply
      0
      • M Offline
        M Offline
        mugunth
        wrote on last edited by
        #3

        Thanks for the reply.
        I found issue with the single quote, but I made it working with slight modification in the jsondata creation as follows:

        QString jsondata("{"age": 35}");

        Also I could make it working by creating QJsonDocument for the variables and using it in the request as follows which was suitable for my case:

        QVariantMap testMapData["age"] = 35;
        QJsonDocument testJsonData = QJsonDocument::fromVariant(testmapdata);

        QNetworkAccessmanager mngr;

        QNetworkRequest request("http://my.org/memebr/14");
        request.setHeader(QNetworkRequest::ContentTypeHeader, QString("application/json"));

        QNetworkReply* reply = mngr.put(request, testJsonData.toJson());

        Thanks again for your reply.

        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