Logo Pico-Framework A web-first embedded framework for C++
Loading...
Searching...
No Matches
JsonResponse.cpp
Go to the documentation of this file.
1
18#include "http/JsonResponse.h"
19#include "http/HttpResponse.h"
20
21namespace JsonResponse {
22
23void sendSuccess(HttpResponse& res, const nlohmann::json& data, const std::string& message) {
24 nlohmann::json j = { {"success", true} };
25 if (!data.is_null() && !data.empty()) j["data"] = data;
26 if (!message.empty()) j["message"] = message;
27 res.status(200).json(j);
28}
29
30void sendCreated(HttpResponse& res, const nlohmann::json& data, const std::string& message) {
31 nlohmann::json j = { {"success", true} };
32 if (!data.is_null() && !data.empty()) j["data"] = data;
33 if (!message.empty()) j["message"] = message;
34 res.status(201).json(j);
35}
36
37void sendMessage(HttpResponse& res, const std::string& message) {
38 res.status(200).json({
39 {"success", true},
40 {"message", message}
41 });
42}
43
45 res.status(204).send("");
46}
47
48void sendJson(HttpResponse& res, const nlohmann::json& raw, int statusCode) {
49 res.status(statusCode).json(raw);
50}
51
52void sendError(HttpResponse& res, int statusCode, const std::string& code, const std::string& message) {
53 res.status(statusCode).json({
54 {"success", false},
55 {"error", {
56 {"code", code},
57 {"message", message}
58 }}
59 });
60}
61
62} // namespace JsonResponse
63
64HttpResponse& HttpResponse::sendSuccess(const nlohmann::json& data, const std::string& message) {
65 JsonResponse::sendSuccess(*this, data, message);
66 return *this;
67}
68
69HttpResponse& HttpResponse::sendCreated(const nlohmann::json& data, const std::string& message) {
70 JsonResponse::sendCreated(*this, data, message);
71 return *this;
72}
73
74HttpResponse& HttpResponse::sendMessage(const std::string& message) {
75 JsonResponse::sendMessage(*this, message);
76 return *this;
77}
78
83
84HttpResponse& HttpResponse::sendError(int statusCode, const std::string& code, const std::string& message) {
85 JsonResponse::sendError(*this, statusCode, code, message);
86 return *this;
87}
HTTP HttpResponse class for managing status, headers, body, and streaming support.
Utility functions to send standard JSON responses using nlohmann::json.
Represents an HTTP response object.
HttpResponse & sendCreated(const nlohmann::json &data={}, const std::string &message="")
HttpResponse & json(const std::string &jsonString)
Send a JSON string/object with correct content type.
void send(const std::string &body)
Send a full response including headers and body.
HttpResponse & status(int code)
Set the HTTP status code.
HttpResponse & sendMessage(const std::string &message)
HttpResponse & sendNoContent()
HttpResponse & sendSuccess(const nlohmann::json &data={}, const std::string &message="")
HttpResponse & sendError(int statusCode, const std::string &code, const std::string &message)
void sendMessage(HttpResponse &res, const std::string &message)
void sendError(HttpResponse &res, int statusCode, const std::string &code, const std::string &message)
void sendNoContent(HttpResponse &res)
void sendSuccess(HttpResponse &res, const nlohmann::json &data, const std::string &message)
void sendJson(HttpResponse &res, const nlohmann::json &raw, int statusCode)
void sendCreated(HttpResponse &res, const nlohmann::json &data, const std::string &message)