33{
34 std::cout << "=== Simple HTTP Client Demo ===" << std::endl;
35
36
37 auto client = std::make_shared<core::http_client>(std::chrono::seconds(30));
38
39
40 std::this_thread::sleep_for(std::chrono::milliseconds(500));
41
42
43 std::cout << "\n1. GET request to /" << std::endl;
44 std::cout << std::string(50, '-') << std::endl;
45
46 auto response1 =
client->get(
"http://localhost:8080/");
47 if (response1.is_ok())
48 {
50 }
51 else
52 {
53 std::cerr << "Request failed: " << response1.error().message << std::endl;
54 }
55
56
57 std::cout << "\n2. GET request with query parameters" << std::endl;
58 std::cout << std::string(50, '-') << std::endl;
59
60 std::map<std::string, std::string> query_params = {
61 {"name", "Alice"}
62 };
63
64 auto response2 =
client->get(
"http://localhost:8080/api/hello", query_params);
65 if (response2.is_ok())
66 {
68 }
69 else
70 {
71 std::cerr << "Request failed: " << response2.error().message << std::endl;
72 }
73
74
75 std::cout << "\n3. GET request with path parameter" << std::endl;
76 std::cout << std::string(50, '-') << std::endl;
77
78 auto response3 =
client->get(
"http://localhost:8080/users/42");
79 if (response3.is_ok())
80 {
82 }
83 else
84 {
85 std::cerr << "Request failed: " << response3.error().message << std::endl;
86 }
87
88
89 std::cout << "\n4. POST request" << std::endl;
90 std::cout << std::string(50, '-') << std::endl;
91
92 std::string post_body = "Hello from HTTP client!";
93 auto response4 =
client->post(
"http://localhost:8080/api/echo", post_body);
94 if (response4.is_ok())
95 {
97 }
98 else
99 {
100 std::cerr << "Request failed: " << response4.error().message << std::endl;
101 }
102
103
104 std::cout << "\n5. Testing 404 Not Found" << std::endl;
105 std::cout << std::string(50, '-') << std::endl;
106
107 auto response5 =
client->get(
"http://localhost:8080/nonexistent");
108 if (response5.is_ok())
109 {
111 }
112 else
113 {
114 std::cerr << "Request failed: " << response5.error().message << std::endl;
115 }
116
117 std::cout << "\n=== Demo complete ===" << std::endl;
118
119 return 0;
120}
@ client
Client-side request.
void print_response(const internal::http_response &response)