QNetworkReply *Requester::sendCustomRequest(QNetworkRequest &request, const QString &type, const QJsonObject &json) { request.setRawHeader("HTTP", type.toUtf8()); QByteArray dataByteArray = jsonToByteArray(json); QBuffer *buff = new QBuffer(); buff->open(QIODevice::ReadWrite); buff->write(dataByteArray); buff->seek(0); QNetworkReply* reply = pimpl->networkAccessManager .sendCustomRequest(request, type.toUtf8(), buff); buff->setParent(reply); return reply;}inlineQByteArray jsonToByteArray(const QJsonObject &json) { return QJsonDocument(json).toJson();}
C++ (Qt)auto buf = new QBuffer;buf->buffer() = data; // QByteArrayauto deleteBufLambda = [buf]{ delete buf; }; if (buf->open(QIODevice::ReadOnly)){ auto reply = _qnam->sendCustomRequest(request, "PATCH", buf); connect(reply, &QNetworkReply::finished, [deleteBufLambda]{ deleteBufLambda(); });}else deleteBufLambda();
C++ (Qt)QNetworkReply* sendCustomRequest(QNetworkRequest &request, const QString &type, const QJsonObject &json) { auto buffer = new QBuffer; buffer->buffer() = jsonToByteArray(json); QNetworkReply *reply; if (buffer->open(QIODevice::ReadOnly)) { reply = pimpl->networkAccessManager .sendCustomRequest(request, type.toUtf8(), buffer); connect(reply, &QNetworkReply::finished, buffer, &QBuffer::deleteLater); } else { buffer->deleteLater(); } return reply;} inlineQByteArray jsonToByteArray(const QJsonObject &json) { return QJsonDocument(json).toJson();}