【问题标题】:Get json from Wt::Http::Request& request从 Wt::Http::Request& 请求获取 json
【发布时间】:2017-02-20 15:04:35
【问题描述】:

我的 json 请求有问题 :( 我有课

class ForumCreate : public Wt::WResource 

和功能

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

request.contentType() 是应用程序/json。 如何从请求中获取 json?(

也许我应该使用其他东西来获取 json? 任务:用户在静态 url 上发送带有 json 的 http-request。我需要分析 json 文件并发送 json-response。

【问题讨论】:

  • 这只是一个简单的猜测,但 handleRequest 可能是针对 http 服务器的。我希望你会想要一个只需要一个 Http::Response 对象的东西,而不是一个请求。
  • 是的,我有 http-server。用户在此服务器上发送 http 请求。并且函数handleRequest正在分析这个请求。

标签: c++ json wt


【解决方案1】:

Wt 中有一个内置的 JSON 解析器。我是这样使用的:

Wt::Json::Object bodyContent;

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

其中 fromIstream 如下:

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

请记住,Wt::Json::parse() 将在输入格式错误的情况下抛出异常。希望对您有所帮助!

【讨论】:

    【解决方案2】:

    您将需要从

    提供的输入流中解析数据
    std::istream & Wt::Http::Request::in    (       )   const
    

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

    它应该是原始的 json 文本。

    【讨论】:

    • std::string temp7; while (std::getline(request.in(),temp7));
    猜你喜欢
    • 2015-02-22
    • 2017-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-10
    相关资源
    最近更新 更多