-
Notifications
You must be signed in to change notification settings - Fork 0
/
CasablancaConsumer.cpp
92 lines (80 loc) · 3.11 KB
/
CasablancaConsumer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// CasablancaConsumer.cpp : Defines the entry point for the console application.
//Reference: Marius Bancila's Blog https://mariusbancila.ro/blog/2017/11/19/revisited-full-fledged-client-server-example-with-c-rest-sdk-2-10/
#include "stdafx.h" //used for windows
#include <cpprest/http_client.h>
#include <cpprest/filestream.h>
#include <cpprest/http_listener.h> // HTTP server
#include <cpprest/json.h> // JSON library
#include <cpprest/uri.h> // URI library
#include <cpprest/ws_client.h> // WebSocket client
#include <cpprest/containerstream.h> // Async streams backed by STL containers
#include <cpprest/interopstream.h> // Bridges for integrating Async streams with STL and WinRT streams
#include <cpprest/rawptrstream.h> // Async streams backed by raw pointer to memory
#include <cpprest/producerconsumerstream.h> // Async streams for producer consumer scenarios
#include <cpprest/http_client.h>
#include <cpprest/filestream.h>
using namespace std;
using namespace utility; // Common utilities like string conversions
using namespace web; // Common features like URIs.
using namespace web::http; // Common HTTP functionality
using namespace web::http::client; // HTTP client features
using namespace concurrency::streams; // Asynchronous streams
pplx::task<int> Post();
void make_request(http_client & client, method mtd, json::value const & jvalue);
void display_field_map_json(json::value & jvalue);
pplx::task<http_response> make_task_request(http_client & client,
method mtd,
json::value const & jvalue);
int _tmain(int argc, _TCHAR* argv[])
{
http_client client(U("http://urlOfServer/WebService.php"));
wcout << L"\npost values (POST)\n";
///hash keys and values for json object here
std::vector<std::pair<utility::string_t, json::value>> putvalue;
putvalue.push_back(make_pair(L"name", json::value(L"Roy Rogers")));
make_request(client, methods::POST, web::json::value::object(putvalue));
return 0;
}
void display_field_map_json(json::value & jvalue)
{
if (!jvalue.is_null())
{
for (auto const & e : jvalue.as_object())
{
wcout << e.first << L" : " << e.second.as_string() << endl;
//parse data here
}
}
}
pplx::task<http_response> make_task_request(http_client & client,
method mtd,
json::value const & jvalue)
{
return (mtd == methods::GET || mtd == methods::HEAD) ?
client.request(mtd, L"") :
client.request(mtd, L"", jvalue);
}
void make_request(http_client & client, method mtd, json::value const & jvalue)
{
make_task_request(client, mtd, jvalue)
.then([](http_response response)
{
if (response.status_code() == status_codes::OK)
{
return response.extract_json();
}
return pplx::task_from_result(json::value());
})
.then([](pplx::task<json::value> previousTask)
{
try
{
display_field_map_json(previousTask.get());
}
catch (http_exception const & e)
{
wcout << e.what() << endl;
}
})
.wait();
}