简体   繁体   中英

Get json from Wt::Http::Request& request

I have problem with json request :( I have class

class ForumCreate : public Wt::WResource 

and function

virtual void handleRequest(const Wt::Http::Request& request, Wt::Http::Response& response)

request.contentType() is application/json. How do I get json from request?(

Maybe I should use something else to get json? Task: User send http-request with json on static url. I need to analize json file and send json-response.

You're going to need to parse the data from the input stream provided by

std::istream & Wt::Http::Request::in    (       )   const

https://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1Http_1_1Request.html#a768a65ceb3c0bf013b57c3de04b19041

It should be the raw json text.

There's a built-in JSON parser in Wt. I use it like this:

Wt::Json::Object bodyContent;

try
{
    Wt::Json::parse(fromIstream(request.in()), bodyContent);
}
catch(std::exception e)
{
    ...
}

Where fromIstream is the following:

std::string fromIstream(std::istream &stream)
{
    std::istreambuf_iterator<char> eos;
    return std::string(std::istreambuf_iterator<char>(stream), eos);
}

Keep in mind that Wt::Json::parse() will throw an exception in case of malformed input. Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM