Logo Pico-Framework A web-first embedded framework for C++
Loading...
Searching...
No Matches
JsonResponse Namespace Reference

Functions

void sendSuccess (HttpResponse &res, const nlohmann::json &data, const std::string &message)
 
void sendCreated (HttpResponse &res, const nlohmann::json &data, const std::string &message)
 
void sendMessage (HttpResponse &res, const std::string &message)
 
void sendNoContent (HttpResponse &res)
 
void sendJson (HttpResponse &res, const nlohmann::json &raw, int statusCode)
 
void sendError (HttpResponse &res, int statusCode, const std::string &code, const std::string &message)
 
void sendNotFound (HttpResponse &res, const std::string &message)
 
void sendBadRequest (HttpResponse &res, const std::string &message)
 
void sendUnauthorized (HttpResponse &res, const std::string &message)
 

Function Documentation

◆ sendBadRequest()

void JsonResponse::sendBadRequest ( HttpResponse res,
const std::string &  message 
)
inline

Definition at line 32 of file JsonResponse.h.

32 {
33 sendError(res, 400, "bad_request", message);
34}
void sendError(HttpResponse &res, int statusCode, const std::string &code, const std::string &message)

References sendError().

+ Here is the call graph for this function:

◆ sendCreated()

void JsonResponse::sendCreated ( HttpResponse res,
const nlohmann::json &  data,
const std::string &  message 
)

Definition at line 30 of file JsonResponse.cpp.

30 {
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}
HttpResponse & json(const std::string &jsonString)
Send a JSON string/object with correct content type.
HttpResponse & status(int code)
Set the HTTP status code.

References HttpResponse::json(), and HttpResponse::status().

Referenced by HttpResponse::sendCreated().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ sendError()

void JsonResponse::sendError ( HttpResponse res,
int  statusCode,
const std::string &  code,
const std::string &  message 
)

Definition at line 52 of file JsonResponse.cpp.

52 {
53 res.status(statusCode).json({
54 {"success", false},
55 {"error", {
56 {"code", code},
57 {"message", message}
58 }}
59 });
60}

References HttpResponse::json(), and HttpResponse::status().

Referenced by HttpServer::handleClient(), sendBadRequest(), HttpResponse::sendError(), HttpResponse::sendError(), sendNotFound(), sendUnauthorized(), and FileHandler::serveFile().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ sendJson()

void JsonResponse::sendJson ( HttpResponse res,
const nlohmann::json &  raw,
int  statusCode 
)

Definition at line 48 of file JsonResponse.cpp.

48 {
49 res.status(statusCode).json(raw);
50}

References HttpResponse::json(), and HttpResponse::status().

+ Here is the call graph for this function:

◆ sendMessage()

void JsonResponse::sendMessage ( HttpResponse res,
const std::string &  message 
)

Definition at line 37 of file JsonResponse.cpp.

37 {
38 res.status(200).json({
39 {"success", true},
40 {"message", message}
41 });
42}

References HttpResponse::json(), and HttpResponse::status().

Referenced by HttpResponse::sendMessage().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ sendNoContent()

void JsonResponse::sendNoContent ( HttpResponse res)

Definition at line 44 of file JsonResponse.cpp.

44 {
45 res.status(204).send("");
46}
void send(const std::string &body)
Send a full response including headers and body.

References HttpResponse::send(), and HttpResponse::status().

Referenced by GpioController::handleGetMultipleGpios(), and HttpResponse::sendNoContent().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ sendNotFound()

void JsonResponse::sendNotFound ( HttpResponse res,
const std::string &  message 
)
inline

Definition at line 29 of file JsonResponse.h.

29 {
30 sendError(res, 404, "not_found", message);
31}

References sendError().

+ Here is the call graph for this function:

◆ sendSuccess()

void JsonResponse::sendSuccess ( HttpResponse res,
const nlohmann::json &  data,
const std::string &  message 
)

Definition at line 23 of file JsonResponse.cpp.

23 {
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}

References HttpResponse::json(), and HttpResponse::status().

Referenced by HttpResponse::sendSuccess().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ sendUnauthorized()

void JsonResponse::sendUnauthorized ( HttpResponse res,
const std::string &  message 
)
inline

Definition at line 35 of file JsonResponse.h.

35 {
36 sendError(res, 401, "unauthorized", message);
37}

References sendError().

+ Here is the call graph for this function: