Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QNetworkAccessManager PUT request example needed

Tags:

rest

qt

qt5

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.

like image 442
Mugunth Avatar asked Aug 15 '26 09:08

Mugunth


1 Answers

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());
like image 122
Mugunth Avatar answered Aug 17 '26 23:08

Mugunth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!