Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
simple_http_client.cpp File Reference
#include <internal/http/http_client.h>
#include <iostream>
#include <thread>
#include <chrono>
Include dependency graph for simple_http_client.cpp:

Go to the source code of this file.

Functions

void print_response (const internal::http_response &response)
 
int main ()
 

Function Documentation

◆ main()

int main ( )

Definition at line 32 of file simple_http_client.cpp.

33{
34 std::cout << "=== Simple HTTP Client Demo ===" << std::endl;
35
36 // Create HTTP client directly for HTTP-specific features
37 auto client = std::make_shared<core::http_client>(std::chrono::seconds(30));
38
39 // Wait a moment for server to be ready (if running locally)
40 std::this_thread::sleep_for(std::chrono::milliseconds(500));
41
42 // Test 1: GET request to root
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 {
49 print_response(response1.value());
50 }
51 else
52 {
53 std::cerr << "Request failed: " << response1.error().message << std::endl;
54 }
55
56 // Test 2: GET request with query parameters
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 {
67 print_response(response2.value());
68 }
69 else
70 {
71 std::cerr << "Request failed: " << response2.error().message << std::endl;
72 }
73
74 // Test 3: GET request with path parameter
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 {
81 print_response(response3.value());
82 }
83 else
84 {
85 std::cerr << "Request failed: " << response3.error().message << std::endl;
86 }
87
88 // Test 4: POST request
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 {
96 print_response(response4.value());
97 }
98 else
99 {
100 std::cerr << "Request failed: " << response4.error().message << std::endl;
101 }
102
103 // Test 5: 404 Not Found
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 {
110 print_response(response5.value());
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}
void print_response(const internal::http_response &response)

References print_response().

Here is the call graph for this function:

◆ print_response()

void print_response ( const internal::http_response & response)

Definition at line 20 of file simple_http_client.cpp.

21{
22 std::cout << "Status: " << response.status_code << " " << response.status_message << std::endl;
23 std::cout << "Headers:" << std::endl;
24 for (const auto& [name, value] : response.headers)
25 {
26 std::cout << " " << name << ": " << value << std::endl;
27 }
28 std::cout << "Body (" << response.body.size() << " bytes):" << std::endl;
29 std::cout << response.get_body_string() << std::endl;
30}
std::map< std::string, std::string > headers
Definition http_types.h:166
auto get_body_string() const -> std::string
Get body as string.

References kcenon::network::internal::http_response::body, kcenon::network::internal::http_response::get_body_string(), kcenon::network::internal::http_response::headers, kcenon::network::internal::http_response::status_code, and kcenon::network::internal::http_response::status_message.

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function: