cpprestsdk服务通过Postman请求返回500错误排查
cpprestsdk HTTP服务请求返回500错误问题
问题背景
- 参考指定博客示例开发基于cpprestsdk的HTTP服务,尝试通过Postman发送POST、GET、PUT、DELETE类请求时,服务返回500内部错误状态码,需要定位问题诱因。
- 开发参考的博客地址:
https://mariusbancila.ro/blog/2017/11/19/revisited-full-fledged-client-server-example-with-c-rest-sdk-2-10/ - 关联参考材料:Postman请求配置界面截图地址
https://i.sstatic.net/koEde.png、Postman请求体设置界面截图地址https://i.sstatic.net/LJLUS.png
最小可复现代码
#include <cpprest/http_listener.h> #include <cpprest/json.h> #pragma comment(lib, "cpprest_2_10") using namespace web; using namespace web::http; using namespace web::http::experimental::listener; #include <iostream> #include <map> #include <set> #include <string> using namespace std; void handle_request( http_request request, function<void(json::value&)> action) { auto answer = json::value::object(); request .extract_json() .then([&answer, &action](pplx::task<json::value> task) { try { auto const& jvalue = task.get(); wcout << answer.serialize() << endl; if (!jvalue.is_null()) { action( answer); } } catch (http_exception const& e) { wcout << e.what() << endl; } }) .wait(); wcout << answer.serialize() << endl; request.reply(status_codes::OK, answer); } void handle_post(http_request request) { std::cout << "Something received" << std::endl; handle_request( request, [](json::value& answer) { answer[L"random"] = json::value::string(L"success"); }); } int main() { http_listener listener(L"http://localhost/restdemo"); listener.support(methods::POST, handle_post); try { listener .open() .then([&listener]() { std::cout << "LISTENING" << std::endl; }) .wait(); while (true); } catch (exception const& e) { wcout << e.what() << endl; } return 0; }
调试定位信息
- 运行最小复现程序时,服务端仅能打印
Something received日志 - 经调试确认,错误触发位置为
auto const& jvalue = task.get();代码行,该行之前的调试逻辑可正常执行,该行之后的逻辑无法运行
内容的提问来源于stack exchange,提问作者special
相关产品推荐
相关产品推荐

