[英]Get json from Wt::Http::Request& request
I have problem with json request :( I have class我有 json 请求的问题 :( 我有课
class ForumCreate : public Wt::WResource
and function和功能
virtual void handleRequest(const Wt::Http::Request& request, Wt::Http::Response& response)
request.contentType() is application/json. request.contentType() 是应用程序/json。 How do I get json from request?(我如何从请求中获取 json?(
Maybe I should use something else to get json?也许我应该使用其他东西来获取 json? Task: User send http-request with json on static url.任务:用户在静态 url 上使用 json 发送 http 请求。 I need to analize json file and send json-response.我需要分析 json 文件并发送 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 https://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1Http_1_1Request.html#a768a65ceb3c0bf013b57c3de04b19041
It should be the raw json text.它应该是原始的 json 文本。
There's a built-in JSON parser in Wt. Wt 中有一个内置的 JSON 解析器。 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: fromIstream 的位置如下:
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.请记住,如果输入格式错误,Wt::Json::parse() 将抛出异常。 Hope it helps!希望能帮助到你!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.