[英]Get json from Wt::Http::Request& request
我有 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 請求。 我需要分析 json 文件並發送 json-response。
您將需要從提供的輸入流中解析數據
std::istream & Wt::Http::Request::in ( ) const
它應該是原始的 json 文本。
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() 將拋出異常。 希望能幫助到你!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.