I have been using QNetworkAccessManager successfully for get and post requests.
But I need to use PUT request to update and external API. This PUT requests needs a json string as part its body to be sent to update a record.
I could not find any working example of a PUT Request using QNetworkAccessManager.
Please help to share a sample code.
Finally I found a way to send Json data in QNetworkAccessManager PUT request.
I formed the required json data and stored that in a QJsonDocument variable. And passed that variable(after converting to utf-8 encoded json document using toJson() ) in the put request.
Here is the working test code I use which I believe will be helpful for somebody attempting this.
QVariantMap testMapData["age"] = 35;
QJsonDocument testJsonData = QJsonDocument::fromVariant(testMapData);
QNetworkAccessManager manager;
QNetworkRequest request(QUrl("http://example/member/14"));
request.setHeader(QNetworkRequest::ContentTypeHeader, QString("application/json"));
QNetworkReply* reply = manager.put(request, testJsonData.toJson());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With