From 6a90fabbf7d2ef4f8900b97422b80d21eb236b19 Mon Sep 17 00:00:00 2001 From: Thomas Piccirello Date: Wed, 24 Jul 2024 15:57:53 -0700 Subject: [PATCH] Send 204 when WebAPI response contains no data Signed-off-by: Thomas Piccirello --- src/webui/webapplication.cpp | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/src/webui/webapplication.cpp b/src/webui/webapplication.cpp index ddcf391768e2..6351ab8f2e1b 100644 --- a/src/webui/webapplication.cpp +++ b/src/webui/webapplication.cpp @@ -358,25 +358,32 @@ void WebApplication::doProcessRequest() try { const APIResult result = controller->run(action, m_params, data); - switch (result.data.userType()) + if (result.data.isNull()) { - case QMetaType::QJsonDocument: - print(result.data.toJsonDocument().toJson(QJsonDocument::Compact), Http::CONTENT_TYPE_JSON); - break; - case QMetaType::QByteArray: + status(204); + } + else + { + switch (result.data.userType()) { - const auto resultData = result.data.toByteArray(); - print(resultData, (!result.mimeType.isEmpty() ? result.mimeType : Http::CONTENT_TYPE_TXT)); - if (!result.filename.isEmpty()) + case QMetaType::QJsonDocument: + print(result.data.toJsonDocument().toJson(QJsonDocument::Compact), Http::CONTENT_TYPE_JSON); + break; + case QMetaType::QByteArray: { - setHeader({u"Content-Disposition"_s, u"attachment; filename=\"%1\""_s.arg(result.filename)}); + const auto resultData = result.data.toByteArray(); + print(resultData, (!result.mimeType.isEmpty() ? result.mimeType : Http::CONTENT_TYPE_TXT)); + if (!result.filename.isEmpty()) + { + setHeader({u"Content-Disposition"_s, u"attachment; filename=\"%1\""_s.arg(result.filename)}); + } } + break; + case QMetaType::QString: + default: + print(result.data.toString(), Http::CONTENT_TYPE_TXT); + break; } - break; - case QMetaType::QString: - default: - print(result.data.toString(), Http::CONTENT_TYPE_TXT); - break; } } catch (const APIError &error)