Skip to content

Commit dfd93c5

Browse files
committed
Add basic example for the http client
1 parent c6c7ab5 commit dfd93c5

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

examples/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
set(EXAMPLE_SOURCES echo_server.cpp daytime_client.cpp threadpool.cpp time_client.cpp)
22

3+
if (WITH_HTTP)
4+
set(EXAMPLE_SOURCES ${EXAMPLE_SOURCES} http_client.cpp)
5+
endif()
6+
37
foreach (examplesource ${EXAMPLE_SOURCES})
48
string(REPLACE ".cpp" "" examplename ${examplesource})
59
add_executable(${examplename} ${examplesource})

examples/http_client.cpp

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,35 @@
1-
//
2-
// Created by lpcvoid on 25.12.22.
3-
//
1+
#include "../src/netlib.hpp"
2+
#include "../src/http/client.hpp"
3+
#include "../src/http/http.hpp"
4+
#include <csignal>
5+
#include <iomanip>
6+
#include <iostream>
7+
8+
9+
void exit_handler(int s){
10+
std::cout << "Goodbye!" << std::endl;
11+
exit(EXIT_SUCCESS);
12+
}
13+
14+
int main(int argc, char** argv)
15+
{
16+
netlib::http::http_client client;
17+
18+
auto res = client.get("http://example.com");
19+
20+
if (res.second) {
21+
std::cerr << "Error: " << res.second.message() << std::endl;
22+
exit(1);
23+
}
24+
25+
std::cout << "Got HTTP response: " << res.first->response_code <<
26+
", version " << res.first->version.first << "." << res.first->version.second << std::endl;
27+
std::cout << "Header entries:" << std::endl;
28+
std::for_each(res.first->headers.begin(), res.first->headers.end(), [](auto header_entry) {
29+
std::cout << header_entry.first << " = " << header_entry.second << std::endl;
30+
});
31+
std::cout << "Body:" << std::endl;
32+
std::cout << res.first.value().body << std::endl;
33+
34+
signal(SIGINT, exit_handler);
35+
}

0 commit comments

Comments
 (0)