| 123456789101112131415161718192021 |
- #include "json_http.hpp"
- #include "errors.hpp"
- namespace svapi {
- nlohmann::json bodyJson(const httplib::Request& req) {
- if (req.body.empty()) return nlohmann::json::object();
- try {
- return nlohmann::json::parse(req.body);
- } catch (const std::exception& e) {
- throw ApiError(ErrCode::BadRequest, "invalid_json",
- std::string("request body is not valid JSON: ") + e.what());
- }
- }
- void sendJson(httplib::Response& res, int status, const nlohmann::json& body) {
- res.status = status;
- res.set_content(body.dump(), "application/json");
- }
- } // namespace svapi
|