使用C REST SDK(Casablanca)的HTTP_CLIENT POST请求
Http_client post request using C++ REST SDK (Casablanca)
我正在尝试使用C REST SDK(Casablanca)库执行POST HTTP请求,但我不再成功...也没有找到任何最近/工作的摘要。有人可以帮我吗?
使用以下代码,我获得了运行时 web :: json :: json_exception 说"不是字符串":
json::value postData;
postData[L"name"] = json::value::string(L"Joe Smith");
postData[L"sport"] = json::value::string(L"Baseball");
web::http::client::http_client client(L"https://jsonplaceholder.typicode.com/posts");
try
{
client.request(
methods::POST,
L"",
postData/*.as_string().c_str()*/,
L"application/json");
}
catch (web::json::json_exception &je)
{
std::cout << je.what();
}
catch (std::exception &e)
{
std::cout << e.what();
}
类似的事情将为您做:
web::json::value json_return;
web::json::value json_v ;
json_v["title"] = web::json::value::string("foo");
json_v["body"] = web::json::value::string("bar");
json_v["userId"] = web::json::value::number(1);
web::http::client::http_client client("https://jsonplaceholder.typicode.com/posts");
client.request(web::http::methods::POST, U("/"), json_v)
.then([](const web::http::http_response& response) {
return response.extract_json();
})
.then([&json_return](const pplx::task<web::json::value>& task) {
try {
json_return = task.get();
}
catch (const web::http::http_exception& e) {
std::cout << "error " << e.what() << std::endl;
}
})
.wait();
std::cout << json_return.serialize() << std::endl;
您也可以简单地解析字符串:
web::json::value json_par;
json_par.parse("{"title": "foo", "body": "bar", "userId": 1}");
在使用JSON对象之后,与第一个示例相同。如果您从文件中读取JSON,则略容易。
相关文章:
- 如何在boost beast http请求中设置http头
- 在多个核心中处理一个HTTP请求
- SFML library: http request
- 使用 Winsock2.h C++向不和谐 API 发送 HTTP 请求时出现问题
- 通过单独的 tcp 流建立 http 连接
- 使用 winsock 接收 http 请求
- 如何在 c++ 中通过 http 发送大型视频文件?
- 对于 http 请求,python 比 c++ 快吗?
- 使用 c++ 的 http 客户端的默认方法
- FindPOCO CMAKE 脚本无法找到 MYSQL::client
- 如何使用C++和Boost Asio从HTTP发布请求中获取键值
- 如何使用boost/beast从HTTP POST请求中解析和提取有效负载?
- 如何重用 http::beast::flat_buffer 和 http::response?
- 在客户端和备忘录中获取 HTTP 命令
- 在C ++ ASIO中,如何为HTTP或HTTPS创建相同的套接字对象(接口/抽象问题?
- HTTP 响应格式不正确?
- 在 C++/C 中使用 CURL 发出带有数据文件的 GET HTTP 请求
- Boost asio library for networking (http client)
- Boost ASIO HTTP client POST
- QTcpServer: Send HTTP/1.0 200 OK to connected client