Skip to content

Commit

Permalink
Send 204 when WebAPI response contains no data
Browse files Browse the repository at this point in the history
  • Loading branch information
Piccirello committed Nov 27, 2024
1 parent 9183b57 commit 78fbcab
Showing 1 changed file with 31 additions and 24 deletions.
55 changes: 31 additions & 24 deletions src/webui/webapplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -359,36 +359,43 @@ 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;
}

switch (result.status)
{
case APIStatus::Async:
status(202);
break;
case APIStatus::Ok:
default:
status(200);
break;
switch (result.status)
{
case APIStatus::Async:
status(202);
break;
case APIStatus::Ok:
default:
status(200);
break;
}
}
}
catch (const APIError &error)
Expand Down

0 comments on commit 78fbcab

Please sign in to comment.