Skip to content

Commit

Permalink
Send 204 when WebAPI response contains no data
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Piccirello <thomas@piccirello.com>
  • Loading branch information
Piccirello committed Sep 24, 2024
1 parent 4555a46 commit 6a90fab
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions src/webui/webapplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 6a90fab

Please sign in to comment.