Add a means to send a POST request that has an empty body

Actually this has already worked if a nullptr was casted as a
QIODevice*. Add an overload with a nullptr_t type, that does
this behind the scenes.

Fixes: QTBUG-108309
Change-Id: I2d4b17ae94cf4de2c42257d471ef901c8994fee5
Reviewed-by: Juha Vuolle <juha.vuolle@qt.io>
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
This commit is contained in:
Mate Barany 2024-03-04 17:33:50 +01:00
parent cd2e1b0b4b
commit e566a8a968
3 changed files with 66 additions and 0 deletions

View File

@ -856,6 +856,24 @@ QNetworkReply *QNetworkAccessManager::post(const QNetworkRequest &request, const
return reply;
}
/*!
\overload
\since 6.8
Sends the POST request specified by \a request without a body and returns
a new QNetworkReply object.
*/
QNetworkReply *QNetworkAccessManager::post(const QNetworkRequest &request, std::nullptr_t nptr)
{
Q_UNUSED(nptr);
QIODevice *dev = nullptr;
return d_func()->postProcess(createRequest(QNetworkAccessManager::PostOperation,
request,
dev));
}
#if QT_CONFIG(http) || defined(Q_OS_WASM)
/*!
\since 4.8

View File

@ -84,6 +84,7 @@ public:
QNetworkReply *get(const QNetworkRequest &request, const QByteArray &data);
QNetworkReply *post(const QNetworkRequest &request, QIODevice *data);
QNetworkReply *post(const QNetworkRequest &request, const QByteArray &data);
QNetworkReply *post(const QNetworkRequest &request, std::nullptr_t nptr);
QNetworkReply *put(const QNetworkRequest &request, QIODevice *data);
QNetworkReply *put(const QNetworkRequest &request, const QByteArray &data);
QNetworkReply *deleteResource(const QNetworkRequest &request);

View File

@ -270,6 +270,8 @@ private Q_SLOTS:
void postToHttpMultipart_data();
void postToHttpMultipart();
void multipartSkipIndices(); // QTBUG-32534
void postWithoutBody_data();
void postWithoutBody();
#if QT_CONFIG(ssl)
void putToHttps_data();
void putToHttps();
@ -646,6 +648,7 @@ public:
bool stopTransfer = false;
bool checkedContentLength = false;
bool foundContentLength = false;
int contentRead = 0;
int contentLength = 0;
@ -677,6 +680,7 @@ public:
{
contentLength = 0;
receivedData.clear();
foundContentLength = false;
}
protected:
@ -737,6 +741,8 @@ private:
if (index == -1)
return;
foundContentLength = true;
index += sizeof("content-length:") - 1;
const auto end = std::find(receivedData.cbegin() + index, receivedData.cend(), '\r');
auto num = receivedData.mid(index, std::distance(receivedData.cbegin() + index, end));
@ -3062,6 +3068,47 @@ void tst_QNetworkReply::multipartSkipIndices() // QTBUG-32534
multiPart->deleteLater();
}
void tst_QNetworkReply::postWithoutBody_data()
{
QTest::addColumn<bool>("client_data");
QTest::newRow("client_has_data") << true;
QTest::newRow("client_does_not_have_data") << false;
}
void tst_QNetworkReply::postWithoutBody()
{
QFETCH(bool, client_data);
QBuffer buff;
if (client_data) {
buff.setData("Dummy data from client to server");
buff.open(QIODevice::ReadOnly);
}
QByteArray dataFromServerToClient = QByteArray("Some ridiculous dummy data");
QByteArray httpResponse = QByteArray("HTTP/1.0 200 OK\r\nContent-Length: ");
httpResponse += QByteArray::number(dataFromServerToClient.size());
httpResponse += "\r\n\r\n";
httpResponse += dataFromServerToClient;
MiniHttpServer server(httpResponse);
server.doClose = true;
QNetworkRequest request(QUrl("http://localhost:" + QString::number(server.serverPort())));
request.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("text/plain"));
QNetworkReplyPtr reply;
if (client_data)
reply.reset(manager.post(request, &buff));
else
reply.reset(manager.post(request, nullptr));
QVERIFY2(waitForFinish(reply) == Success, msgWaitForFinished(reply));
QCOMPARE(server.foundContentLength, client_data);
}
void tst_QNetworkReply::putToHttpMultipart_data()
{
postToHttpMultipart_data();