22 #include "http/JsonRequestHelper.h"
25 using json = nlohmann::json;
30 if (!req.
isJson())
return json::object();
31 return json::parse(req.
getBody(),
nullptr,
false);
38 if (!current.is_object())
return nullptr;
41 while ((next = path.find(
'.', pos)) != std::string::npos)
43 std::string part = path.substr(pos, next - pos);
44 if (!current.contains(part) || !current[part].is_object())
return nullptr;
45 current = current[part];
49 std::string last = path.substr(pos);
50 return current.contains(last) ? current[last] :
nullptr;
63 if (val.is_string())
return val.get<std::string>();
64 if (!val.is_null())
return val.dump();
72 return val.is_number_integer() ? val.get<
int>() : def;
79 return val.is_number() ? val.get<
double>() : def;
86 return val.is_boolean() ? val.get<
bool>() : def;
93 return val.is_array() ? val : json::array();
100 return val.is_object() ? val : json::object();
Macro-based debug trace system with optional SD file logging.
#define TRACE_INIT(MODULE_NAME)
Declare trace usage in a source file for a given module.
Defines the HttpRequest class for handling HTTP requests: headers, method, path, query string,...
Forward declaration for potential routing needs.
const std::string & getBody() const
Get the request body (copy).
bool isJson() const
Check if content-type is application/json.
Utility class for working with JSON content in HTTP requests.
static std::string getString(const HttpRequest &req, const std::string &key)
Get a string value from the request body.
static bool getBool(const HttpRequest &req, const std::string &key, bool def=false)
Get a boolean value from the request body.
static json getJsonValue(const HttpRequest &req, const std::string &path)
Extract a nested JSON value using a dot-separated path (e.g., "user.name").
static int getInt(const HttpRequest &req, const std::string &key, int def=0)
Get an integer value from the request body.
static json getArray(const HttpRequest &req, const std::string &key)
Get a JSON array from the request body.
static bool hasField(const HttpRequest &req, const std::string &key)
Check if a JSON field exists.
static json getFullJson(const HttpRequest &req)
Parse and return the full JSON body from the request.
static double getDouble(const HttpRequest &req, const std::string &key, double def=0.0)
Get a double value from the request body.
static json getObject(const HttpRequest &req, const std::string &key)
Get a JSON object from the request body.
Delegates to user or system configuration.