Adding body to QNAM post
-
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.
-
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?
@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
-
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.
@ocgltd
I don't think you should be usingQUrlQuery, you need aQNetworkRequest. 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.
-
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); -
@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¶m2=value2A 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()); -
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?
-
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?
@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
-
O ocgltd has marked this topic as solved on