Speedy response can send json document as string (.json methods)
This commit is contained in:
parent
634c44c724
commit
f015ce02ca
@ -33,8 +33,9 @@ public:
|
||||
// [Atom id=k] k {1, 2, ...}
|
||||
//
|
||||
auto node = atom->first();
|
||||
|
||||
// TODO read the JSON body and store the properties in the
|
||||
|
||||
// the json body is parsed (req.json)
|
||||
// TODO store the properties in the
|
||||
// first version
|
||||
//
|
||||
// for(key, value in body)
|
||||
|
2
speedy/commands.txt
Normal file
2
speedy/commands.txt
Normal file
@ -0,0 +1,2 @@
|
||||
# dump of all commands used in development
|
||||
clang++ -std=c++1y -g test_2.cpp -o test_2 -luv -lhttp_parser r3/.libs/libr3.a -lpcre -L/usr/local/lib -I../ -I./rapidjson/include
|
@ -14,8 +14,12 @@ namespace sp
|
||||
|
||||
bool rapidjson_middleware(sp::Request& req, sp::Response& res)
|
||||
{
|
||||
// the body is empty and json parsing isn't necessary
|
||||
if (req.body.empty())
|
||||
return true;
|
||||
|
||||
if (req.json.Parse(req.body.c_str()).HasParseError()) {
|
||||
const char *errorCode = rapidjson::GetParseError_En(req.json.GetParseError());
|
||||
auto errorCode = rapidjson::GetParseError_En(req.json.GetParseError());
|
||||
std::string parseError = "JSON parse error: " + std::string(errorCode);
|
||||
res.send(http::Status::BadRequest, parseError);
|
||||
return false;
|
||||
|
@ -4,6 +4,10 @@
|
||||
#include "request.hpp"
|
||||
#include "http/response.hpp"
|
||||
|
||||
#include "rapidjson/document.h"
|
||||
#include "rapidjson/writer.h"
|
||||
#include "rapidjson/stringbuffer.h"
|
||||
|
||||
namespace sp
|
||||
{
|
||||
|
||||
@ -11,6 +15,28 @@ class Response : public http::Response<Request, Response>
|
||||
{
|
||||
public:
|
||||
using http::Response<Request, Response>::Response;
|
||||
|
||||
std::string json_string(const rapidjson::Document& document)
|
||||
{
|
||||
rapidjson::StringBuffer strbuf;
|
||||
rapidjson::Writer<rapidjson::StringBuffer> writer(strbuf);
|
||||
document.Accept(writer);
|
||||
// TODO: error handling
|
||||
auto str = strbuf.GetString();
|
||||
return str;
|
||||
}
|
||||
|
||||
void json(http::Status code, const rapidjson::Document& document)
|
||||
{
|
||||
auto str = json_string(document);
|
||||
this->send(code, str);
|
||||
}
|
||||
|
||||
void json(const rapidjson::Document& document)
|
||||
{
|
||||
auto str = json_string(document);
|
||||
this->send(str);
|
||||
}
|
||||
};
|
||||
|
||||
using request_cb_t = std::function<void(Request&, Response&)>;
|
||||
|
23
speedy/test_speedy_2_rapidjson.cpp
Normal file
23
speedy/test_speedy_2_rapidjson.cpp
Normal file
@ -0,0 +1,23 @@
|
||||
#include "speedy.hpp"
|
||||
|
||||
#include "request.hpp"
|
||||
#include "response.hpp"
|
||||
#include "rapidjson/document.h"
|
||||
|
||||
int main() {
|
||||
|
||||
uv::UvLoop::sptr loop(new uv::UvLoop());
|
||||
http::Ipv4 ip("0.0.0.0", 8765);
|
||||
sp::Speedy app(loop);
|
||||
|
||||
app.get("/bla", [](sp::Request& req, sp::Response& res) {
|
||||
rapidjson::Document document;
|
||||
document.Parse("{ \"test\": \"test\" }");
|
||||
res.json(http::Status::Ok, document);
|
||||
});
|
||||
|
||||
app.listen(ip);
|
||||
loop->run(uv::UvLoop::Mode::Default);
|
||||
|
||||
return 1;
|
||||
}
|
Loading…
Reference in New Issue
Block a user