【发布时间】:2020-11-17 14:41:04
【问题描述】:
我有来自 example 代码的服务器
当我尝试通过 Postman 发送 post(或 get、put、del)请求时,我得到了 500 个服务器代码响应(内部错误),是什么导致了问题
#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;
}
这是我最小的可复制示例,当我在 Postman 中发布请求时,在服务器端我只看到“收到的东西”行。
我在线测试服务器出错
auto const& jvalue = task.get();
调试行在该行之前工作,在该行之后不工作。
【问题讨论】:
-
除了返回 500 错误之外,您的 HTTP 服务器不会记录错误吗?如果不是,您需要增加日志记录。
-
@Botje 我真的不知道如何从 ccprestsdk 获取日志。你能解释一下怎么做吗?
-
我简单地检查了那个库,它根本不做任何日志记录。这很烦人。建议您为失败的特定端点发布代码的minimal reproducible example。您还应该在创建
InternalError响应的几行上放置一个调试断点。 -
@Botje 我用最少的可复制代码编辑我的 qwestion。你认为呢 ?提前致谢!
-
@Botje 我发现了服务器出错的地方它的“auto const& jvalue = task.get();”
标签: c++ postman cpprest-sdk