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. Adding body to QNAM post
Forum Updated to NodeBB v4.3 + New Features

Adding body to QNAM post

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 669 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.
  • ocgltdO Offline
    ocgltdO Offline
    ocgltd
    wrote on last edited by
    #1

    I currently post to a ReST server in Qt6 like this:

    QNetworkRequest request;
    request.setUrl(QUrl("someurl"));
    QUrlQuery postData;
    postData.addQueryItem("somekey","somevalue");
    nm = new QNetworkAccessManager
    reply = nm->post(request,postData.toString(QUrl::FullyEncoded).toUtf8());
    

    However, I am told that I need to send some JSON in the body of the request. Where do I set the body of the post? The second parameter of the ->post method is already the query parameters.

    I'm guessing I need to combine the body (json document) with the postData but I'm not sure how.

    JonBJ 1 Reply Last reply
    0
    • ocgltdO ocgltd

      Maybe my understanding of what is posted is wrong...so...If I post form-urlencoded data, does that mean that the body is ONLY key-value pairs? Or can I also send some text like "HELLO" in the body?

      So for form-urlencoded you can have parameters on the URL and in the body, but nothing else in the body?

      C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #6

      @ocgltd said in Adding body to QNAM post:

      If I post form-urlencoded data, does that mean that the body is ONLY key-value pairs?

      Yes. The request body is one blob of key value pairs encoded as they would be if placed in the URL.
      Its primary purpose is to transport the field values from a HTML form element.

      Or can I also send some text like "HELLO" in the body?

      If that text is one of the encoded values, then yes.

      If you need to send a request with multiple, potentially independent, portions of different types then you need a multipart form submission

      https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST#examples

      1 Reply Last reply
      0
      • ocgltdO ocgltd

        I currently post to a ReST server in Qt6 like this:

        QNetworkRequest request;
        request.setUrl(QUrl("someurl"));
        QUrlQuery postData;
        postData.addQueryItem("somekey","somevalue");
        nm = new QNetworkAccessManager
        reply = nm->post(request,postData.toString(QUrl::FullyEncoded).toUtf8());
        

        However, I am told that I need to send some JSON in the body of the request. Where do I set the body of the post? The second parameter of the ->post method is already the query parameters.

        I'm guessing I need to combine the body (json document) with the postData but I'm not sure how.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #2

        @ocgltd
        I don't think you should be using QUrlQuery, you need a QNetworkRequest. See e.g. How to send a POST request in Qt with the JSON body I presume that is suitable for REST.

        You perhaps also ought look at RESTful Client Applications in Qt 6.7 and Forward.

        1 Reply Last reply
        1
        • ocgltdO Offline
          ocgltdO Offline
          ocgltd
          wrote on last edited by ocgltd
          #3

          I found this online...while try that approach

          QUrl url("https://example.com/api");
          QUrlQuery query;
          query.addQueryItem("param1", "value1");
          query.addQueryItem("param2", "value2");
          url.setQuery(query);

          1 Reply Last reply
          0
          • C Offline
            C Offline
            ChrisW67
            wrote on last edited by
            #4

            @ocgltd. Your most recent post will generate a URL suitable for a GET request with the parameters encoded in the request URL. In this case something like:

            https://httpbin.org/get?param1=value1&param2=value2
            

            A POST request carries the query parameters or other data in the request body with an appropriate HTTP method and Content-Type header. What the server end requires of that payload is entirely up to them. Your original post suggested that you needed to send JSON data, but your approach is going down the path of sending content of type "application/x-www-form-urlencoded" (i.e. the POST data is essentially the same as the query part of the equivalent GET request). If you need to send several different types of data then QHttpMultiPart could be the way.

            For the JSON case, the link @JonB provided shows an example.
            For the form data case:

            QNetworkAccessManager nm;
            QNetworkRequest request;
            request.setUrl(QUrl("someurl"));
            request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
            QUrlQuery postData;
            postData.addQueryItem("somekey", "somevalue");
            QNetworkReply *reply = nm.post(request, postData.toString(QUrl::FullyEncoded).toUtf8());
            
            1 Reply Last reply
            2
            • ocgltdO Offline
              ocgltdO Offline
              ocgltd
              wrote on last edited by
              #5

              Maybe my understanding of what is posted is wrong...so...If I post form-urlencoded data, does that mean that the body is ONLY key-value pairs? Or can I also send some text like "HELLO" in the body?

              So for form-urlencoded you can have parameters on the URL and in the body, but nothing else in the body?

              C 1 Reply Last reply
              0
              • ocgltdO ocgltd

                Maybe my understanding of what is posted is wrong...so...If I post form-urlencoded data, does that mean that the body is ONLY key-value pairs? Or can I also send some text like "HELLO" in the body?

                So for form-urlencoded you can have parameters on the URL and in the body, but nothing else in the body?

                C Offline
                C Offline
                ChrisW67
                wrote on last edited by
                #6

                @ocgltd said in Adding body to QNAM post:

                If I post form-urlencoded data, does that mean that the body is ONLY key-value pairs?

                Yes. The request body is one blob of key value pairs encoded as they would be if placed in the URL.
                Its primary purpose is to transport the field values from a HTML form element.

                Or can I also send some text like "HELLO" in the body?

                If that text is one of the encoded values, then yes.

                If you need to send a request with multiple, potentially independent, portions of different types then you need a multipart form submission

                https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST#examples

                1 Reply Last reply
                0
                • ocgltdO ocgltd has marked this topic as solved on
                • ocgltdO Offline
                  ocgltdO Offline
                  ocgltd
                  wrote on last edited by
                  #7

                  Now I get it! Good explanation

                  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