2

I have the following code to send POST, but I am getting an error with that even it compile correctly

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());
}

But I am getting the following error when try to run the program

terminate called after throwing an instance of 'web::uri_exception'
what():  provided uri is invalid: {"name":"Mohammad"}
Aborted (core dumped)
  • The [number of overloads](http://microsoft.github.io/cpprestsdk/classweb_1_1http_1_1client_1_1http__client.html) of `client::request` is bewildering, but at a guess, you need to pass a path before the json data. What did you pass to the `http_client` when you created it? perhaps you need to split the path out of that, just leaving the host name and pass the path to `client::request`? – Paul Rooney Apr 12 '17 at 23:45
  • @PaulRooney I created the client as following: http_client client(U("http://l0.71.103.63:34568")); – Mohammad Alhowaidi Apr 13 '17 at 17:15
  • What is the resource you are trying to post to? Can you do it with curl or with any other languages (e.g. Python requests)? You could try a resource of `"/"` but that seems weird. – Paul Rooney Apr 14 '17 at 01:20

2 Answers2

3

I think the problem is your IP address. It looks like your IP address is wrong? You have "http://l0." where the "10" is "l0" (lowercase L).

Hence the web:uri_exception.

Mobile Ben
  • 7,121
  • 1
  • 27
  • 43
0

You are wrong (I do think) in your request, should look like that:

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

The second argument in the request is the URL complement, and you are passing your json in string, hence the error you have.
basically the syntax you want is this one:

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() )