QNetworkRequest + How to add a json text in a PUT REST ?
-
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. -
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());
-
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.