39 lines
1.0 KiB
C++
39 lines
1.0 KiB
C++
#include "messageutils.h"
|
|
|
|
#include <pihttpserver.h>
|
|
|
|
namespace MessageUtils {
|
|
|
|
PIHTTP::MessageMutable jsonReply(PIHTTP::Code code, const PIJSON & json) {
|
|
PIHTTP::MessageMutable msg;
|
|
msg.setCode(code);
|
|
msg.setBody(json.toJSON(PIJSON::Compact).toUTF8());
|
|
msg.addHeader("Content-Type", "application/json; charset=utf-8");
|
|
return msg;
|
|
}
|
|
|
|
PIHTTP::MessageMutable errorReply(PIHTTP::Code code, const PIString & message) {
|
|
PIJSON j = PIJSON::newObject();
|
|
j["error"] = message;
|
|
return jsonReply(code, j);
|
|
}
|
|
|
|
PIHTTP::MessageMutable successReply(const PIJSON & json) {
|
|
return jsonReply(PIHTTP::Code::Ok, json);
|
|
}
|
|
|
|
PIHTTP::MessageMutable noContent() {
|
|
PIHTTP::MessageMutable msg;
|
|
msg.setCode(PIHTTP::Code::NoContent);
|
|
return msg;
|
|
}
|
|
|
|
PIString gatherArgument(const PIHTTP::MessageConst & request, const PIString & key) {
|
|
PIJSON js = PIJSON::fromJSON(PIString::fromUTF8(request.body()));
|
|
if (js.contains(key)) return js[key].toString();
|
|
if (request.arguments().contains(key)) return request.arguments().value(key);
|
|
return {};
|
|
}
|
|
|
|
} // namespace MessageUtils
|