c++ - Linux 上 http_request::set_body 中的 cpprestsdk 段错误

标签 c++ linux cpprest-sdk

我尝试使用 cpprestsdk 向服务器发送 HTTP (REST) 请求。

void postRestRequest(const std::string& uri, const std::string& requestJson) {
  const std::string host = "localhost:8080";
  const http_client_config authorization = setupAuthorization();

  http_client client(U(host), authorization);
  http_request request(methods::POST);
  uri_builder builder(U(uri));
  request.set_request_uri(builder.to_string());

  if (not requestJson.empty()) {
    const auto mimeType = utf8string("application/json; charset=utf-8");
    request.set_body(requestJson, mimeType); // SEGFAULT here
  }
}

调用堆栈是:

-|libpthread.so.0
-|[1]
-|
-|libcpprest.so.2.9
-|[2]    : pplx::task_completion_event<unsigned long>::set(unsigned long) const+0xb4
-|
-|mylib.so
-|[3]    : postRestRequest(std::string const&, std::string const&)+0x49a

我不确定我是否按预期使用了该库。 我不确切知道 pplx 库中的任务是如何工作的。

会不会也是cpprestsdk库的bug。 但是创建 HTTP 请求是 REST 框架的重要组成部分。所以我无法想象库中存在未知错误。

Linux:RHEL7
cpprestsdk: 2.9
海合会:6.3.1

最佳答案

这就是我使用 web::json::value 的方式。请注意,我使用像 value::number 这样的成员函数来为我的请求对象创建有效的 json 值。

web::json::value requestParameters;
requestParameters[U("foo")] = web::json::value::number(1);
requestParameters[U("bar")] = web::json::value::string(U("whatever"));

utility::stringstream_t paramStream;
requestParameters.serialize(paramStream);

web::http::http_request request(web::http::methods::POST);
request.set_request_uri(U("MethodName"));
request.set_body(paramStream.str());

关于c++ - Linux 上 http_request::set_body 中的 cpprestsdk 段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48805739/

相关文章:

c++ - cmake 找不到 boost 库,因为它查找了错误的文件名

c++ - find() STL 算法...我做错了什么?

linux - 关于 UNIX 移动命令覆盖保护

json - 使用 cpprest-sdk 的 json 服务的 POST 问题,连接到 jeasyui

c++ - 如何从字符串创建 web::uri 并将其放入 client.connect() 中?

c++ - 为什么我的输出包含一个 0?

c++ - VS 2012 的全新安装不会构建默认控制台应用程序 : Missing SDKDDKVer. h(和 stdio.h/CRT)

linux - 使用重命名命令批量重命名文件

c - 从 poll() 添加/删除套接字描述符

c++ - 使用 cpprestsdk 将字符串转换为 web::json