SNode.C
Loading...
Searching...
No Matches
jsonclient.cpp File Reference
Include dependency graph for jsonclient.cpp:

Go to the source code of this file.

Functions

int main (int argc, char *argv[])
 

Function Documentation

◆ main()

int main ( int argc,
char * argv[] )

Definition at line 33 of file jsonclient.cpp.

33 {
34 core::SNodeC::init(argc, argv);
35
37 using Request = Client::Request;
38 using Response = Client::Response;
39 using SocketAddress = Client::SocketAddress;
40
41 const Client jsonClient(
42 "legacy",
43 [](const std::shared_ptr<Request>& req) {
44 VLOG(1) << "-- OnRequest";
45 req->method = "POST";
46 req->url = "/index.html";
47 req->type("application/json");
48 req->set("Connection", "close");
49 req->send("{\"userId\":1,\"schnitzel\":\"good\",\"hungry\":false}",
50 []([[maybe_unused]] const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res) {
51 VLOG(1) << "-- OnResponse";
52 VLOG(1) << " Status:";
53 VLOG(1) << " " << res->httpVersion;
54 VLOG(1) << " " << res->statusCode;
55 VLOG(1) << " " << res->reason;
56
57 VLOG(1) << " Headers:";
58 for (const auto& [field, value] : res->headers) {
59 VLOG(1) << " " << field + " = " + value;
60 }
61
62 VLOG(1) << " Cookies:";
63 for (const auto& [name, cookie] : res->cookies) {
64 VLOG(1) << " " + name + " = " + cookie.getValue();
65 for (const auto& [option, value] : cookie.getOptions()) {
66 VLOG(1) << " " + option + " = " + value;
67 }
68 }
69
70 res->body.push_back(0);
71 VLOG(1) << " Body:\n----------- start body -----------" << res->body.data()
72 << "------------ end body ------------";
73 });
74 },
75 []([[maybe_unused]] const std::shared_ptr<Request>& req) {
76 LOG(INFO) << " -- OnRequestEnd";
77 });
78
79 jsonClient.connect("localhost",
80 8080,
81 [instanceName = jsonClient.getConfig().getInstanceName()](
82 const SocketAddress& socketAddress,
83 const core::socket::State& state) { // example.com:81 simulate connect timeout
84 switch (state) {
86 VLOG(1) << instanceName << ": connected to '" << socketAddress.toString() << "'";
87 break;
89 VLOG(1) << instanceName << ": disabled";
90 break;
92 LOG(ERROR) << instanceName << ": " << socketAddress.toString() << ": " << state.what();
93 break;
95 LOG(FATAL) << instanceName << ": " << socketAddress.toString() << ": " << state.what();
96 break;
97 }
98 });
99 /*
100 jsonClient.connect("localhost",
101 8080,
102 [instanceName = jsonClient.getConfig().getInstanceName()](
103 const SocketAddress& socketAddress,
104 const core::socket::State& state) { // example.com:81 simulate connnect timeout
105 switch (state) {
106 case core::socket::State::OK:
107 VLOG(1) << instanceName << ": connected to '" << socketAddress.toString() << "'";
108 break;
109 case core::socket::State::DISABLED:
110 VLOG(1) << instanceName << ": disabled";
111 break;
112 case core::socket::State::ERROR:
113 LOG(ERROR) << instanceName << ": " << socketAddress.toString() << ": " << state.what();
114 break;
115 case core::socket::State::FATAL:
116 LOG(FATAL) << instanceName << ": " << socketAddress.toString() << ": " << state.what();
117 break;
118 }
119 });
120 */
121 /*
122 jsonClient.post("localhost", 8080, "/index.html", "{\"userId\":1,\"schnitzel\":\"good\",\"hungry\":false}", [](int err) {
123 if (err != 0) {
124 PLOG(ERROR) << "OnError: " << err;
125 }
126 });
127 */
128
129 return core::SNodeC::start();
130}
static void init(int argc, char *argv[])
Definition SNodeC.cpp:32
static int start(const utils::Timeval &timeOut={LONG_MAX, 0})
Definition SNodeC.cpp:38
static constexpr int DISABLED
Definition State.h:34
static constexpr int ERROR
Definition State.h:35
static constexpr int FATAL
Definition State.h:36
static constexpr int OK
Definition State.h:33
Client::Response Response
Definition clients.h:65
web::http::legacy::NET::Client Client
Definition clients.h:63
Client::Request Request
Definition clients.h:64
SocketClient::SocketAddress SocketAddress
ClientBase< web::http::client::Request, web::http::client::Response > Client
Definition Client.h:35