PDA

View Full Version : QNetworkAccessManager PUT method delete data over server



npatil15
27th June 2019, 13:43
Hi,

I'm trying to update JSON data over a server but it is not updating, its deleting everything even if I send blank data.

Find the implementation below,



QNetworkAccessManager m_mgr;
QNetworkRequest m_request;
QNetworkReply *m_reply;
QUrl url("https://api.predic8.de/shop/products/10");
m_request.setUrl(url);

QVariantMap data;
data.insert("name", "Oranges");

QJsonDocument jsonData = QJsonDocument::fromVariant(data);
m_reply = m_mgr.put(m_request, jsonData.toJson(QJsonDocument::Indented));
connect(m_reply, &QNetworkReply::finished, m_reply, &QNetworkReply::deleteLater);

Please help me why it delete the data instead of an update?

npatil15
28th June 2019, 08:49
I found the solution,
Setting header solved my issue,

m_request.setHeader( QNetworkRequest::ContentTypeHeader, "application/json" );
Technically I don't understand why its deleting data if I didn't set header.
Can someone explain to me this or does it mandatory to use header?

Thanks

ChristianEhrlicher
28th June 2019, 21:37
Can someone explain to me this or does it mandatory to use header?


How should we know what your 'server' does at all?

npatil15
1st July 2019, 11:51
Hi ChristianEhrlicher,

I'm using a local server, and follow this below tutorial for creating this local server,
https://medium.com/codingthesmartway-com-blog/create-a-rest-api-with-json-server-36da8680136d

ChristianEhrlicher
1st July 2019, 18:04
Since this is not C++/Qt I can't help debugging the server.