I'm trying to send a POST request from Qt where the body is in JSON format. Firstly I'm asserting that the request works in curl:
curl -i -H "Content-Type: application/json" -d '{"key1": "value1", "key2": "value2"}' -X POST http://myserver.com/api
That works OK, I'm receiving back the expected response from the server. The same works in Python. Now I need to send this request from C++/Qt:
QNetworkAccessManager *mgr = new QNetworkAccessManager(this);
QHttpMultiPart *httpMultiPart = new QHttpMultiPart(mgr);
QHttpPart *httpPart = new QHttpPart();
httpPart->setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/json"));
httpPart->setBody("{\"key1\":\"value1\", \"key2\":\"value2\"}");
httpMultiPart->append(*httpPart);
QNetworkReply *reply = mgr->post(QNetworkRequest(QUrl("http://myserver.com/api")), httpMultiPart);
QObject::connect(reply, &QNetworkReply::finished, [=]()
{
QString err = reply->errorString();
QString contents = QString::fromUtf8(reply->readAll());
});
Now I'm getting an error where the errorString returns: `
"Error downloading http://myserver.com/api - server replied: Unsupported Media Type"`
What can be the reason? How should I send the POST request like the one that I'm sending with curl?