SHARE
TWEET

Casa_clnt.cpp

a guest Jun 3rd, 2016 844 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <cpprest/http_client.h>
  2. #include <cpprest/json.h>
  3.  
  4. using namespace web;
  5. using namespace web::http;
  6. using namespace web::http::client;
  7.  
  8. #include <iostream>
  9. using namespace std;
  10.  
  11. void display_field_map_json(json::value & jvalue)
  12. {
  13.     if (!jvalue.is_null())
  14.     {
  15.         for (auto const & e : jvalue.as_object())
  16.         {
  17.             wcout << e.first << L" : " << e.second.as_string() << endl;
  18.         }
  19.     }
  20. }
  21.  
  22. pplx::task<http_response> make_task_request(http_client & client,
  23.     method mtd,
  24.     json::value const & jvalue)
  25. {
  26.     return (mtd == methods::GET || mtd == methods::HEAD) ?
  27.         client.request(mtd, L"/restdemo") :
  28.         client.request(mtd, L"/restdemo", jvalue);
  29. }
  30.  
  31. void make_request(http_client & client, method mtd, json::value const & jvalue)
  32. {
  33.     make_task_request(client, mtd, jvalue)
  34.         .then([](http_response response)
  35.     {
  36.         if (response.status_code() == status_codes::OK)
  37.         {
  38.             return response.extract_json();
  39.         }
  40.         return pplx::task_from_result(json::value());
  41.     })
  42.         .then([](pplx::task<json::value> previousTask)
  43.     {
  44.         try
  45.         {
  46.             display_field_map_json(previousTask.get());
  47.         }
  48.         catch (http_exception const & e)
  49.         {
  50.             wcout << e.what() << endl;
  51.         }
  52.     })
  53.         .wait();
  54. }
  55.  
  56. int main()
  57. {
  58.     http_client client(U("http://localhost"));
  59.  
  60.     std::vector<std::pair<utility::string_t, json::value>> putvalue;
  61.     putvalue.push_back(make_pair(L"one", json::value(L"100")));
  62.     putvalue.push_back(make_pair(L"two", json::value(L"200")));
  63.  
  64.     wcout << L"\nput values\n";
  65.     make_request(client, methods::PUT, json::value::object(putvalue));
  66.  
  67.     auto getvalue = json::value::array();
  68.     getvalue[0] = json::value(L"one");
  69.     getvalue[1] = json::value(L"two");
  70.     getvalue[2] = json::value(L"three");
  71.  
  72.     wcout << L"\nget values (POST)\n";
  73.     make_request(client, methods::POST, getvalue);
  74.  
  75.     auto delvalue = json::value::array();
  76.     delvalue[0] = json::value(L"one");
  77.  
  78.     wcout << L"\ndelete values\n";
  79.     make_request(client, methods::DEL, delvalue);
  80.  
  81.     wcout << L"\nget values (POST)\n";
  82.     make_request(client, methods::POST, getvalue);
  83.  
  84.     wcout << L"\nget values (GET)\n";
  85.     make_request(client, methods::GET, json::value::null());
  86.  
  87.     return 0;
  88. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top