使用 cpprest 发送开机自检

Sending POST using cpprest

本文关键字:开机自检 cpprest 使用      更新时间:2023-10-16

我有以下代码要发送 POST,但即使它编译正确,我也会收到错误

http_client client(U("http://l0.71.103.63:34568"));
json::value postData;
postData["name"] = json::value::string(U("Mohammad"));
http_response response = client.request(methods::POST,postData.to_string().c_str()).get();
if(response.status_code() == status_codes::OK)
{
  auto body = response.extract_string();
  std::wcout << L"Added new Id: " << body.get().c_str() << std::endl;
  return std::stoi(body.get().c_str());
}

但是我在尝试运行程序时收到以下错误

terminate called after throwing an instance of 'web::uri_exception'
what():  provided uri is invalid: {"name":"Mohammad"}
Aborted (core dumped)
我认为

问题是您的IP地址。看起来您的 IP 地址有误?你有"http://l0.",其中">10"是">l0"(小写L(。

因此,网络:uri_exception。

你的请求是错误的(我确实认为(,应该看起来像这样:

auto response = client.request(methods::P OST, U("\"(, postData(.get((;

请求中的第二个参数是 URL 补码,您正在字符串中传递 json,因此会出现错误。
基本上你想要的语法是这样的:

pplx::task web::http::client::http_client::request ( const Method & MTD, const utility::string_t & path_query_fragment, const json::value & body_data, const pplx::cancellation_token & token = pplx::cancellation_token::none(( )