| #include "http/http_request.hpp" |
| #include <boost/algorithm/string/predicate.hpp> |
| #include <nlohmann/json.hpp> |
| enum class JsonParseResult |
| inline JsonParseResult parseRequestAsJson(const crow::Request& req, |
| std::string_view contentType = |
| req.getHeaderValue(boost::beast::http::field::content_type); |
| if (!boost::iequals(contentType, "application/json") && |
| !boost::iequals(contentType, "application/json; charset=utf-8")) |
| BMCWEB_LOG_WARNING << "Failed to parse content type on request"; |
| #ifndef BMCWEB_INSECURE_IGNORE_CONTENT_TYPE |
| return JsonParseResult::BadContentType; |
| jsonOut = nlohmann::json::parse(req.body(), nullptr, false); |
| if (jsonOut.is_discarded()) |
| BMCWEB_LOG_WARNING << "Failed to parse json in request"; |
| return JsonParseResult::BadJsonData; |
| return JsonParseResult::Success; |