-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_example.cc
61 lines (51 loc) · 1.71 KB
/
http_example.cc
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
#include <benoni/http.h>
#if defined(__APPLE__)
#include <CoreFoundation/CoreFoundation.h>
#elif defined(linux)
#include <glib.h>
#endif
#include <iostream>
#include <variant>
using benoni::Method;
using benoni::request;
using benoni::RequestOptions;
using benoni::RequestOptionsBuilder;
using benoni::Response;
int main() {
#if defined(linux)
GMainLoop *loop = g_main_loop_new(nullptr, FALSE);
#endif
std::string url{"https://postman-echo.com/get"};
std::cout << "sending request to: \"" << url << "\"" << std::endl;
request(std::move(url), RequestOptionsBuilder{}.build(),
[](std::variant<std::string, Response> result) {
// This callback is run in a background thread on Windows and Apple
// and on the main thread on Gtk Linux.
if (std::holds_alternative<std::string>(result)) {
std::cerr << "error: [" << std::get<std::string>(result) << "]"
<< std::endl;
exit(EXIT_FAILURE);
}
Response response{std::get<Response>(result)};
std::cout << "response status: " << response.status << std::endl;
std::cout << "response headers: [" << std::endl;
for (const auto &[key, value] : response.headers) {
std::cout << " \"" << key << "\": \"" << value << "\","
<< std::endl;
}
std::cout << "]" << std::endl;
std::cout << "response body: \"" << response.body << "\""
<< std::endl;
exit(EXIT_SUCCESS);
});
#if defined(__APPLE__)
CFRunLoopRun();
#elif defined(linux)
g_main_loop_run(loop);
g_main_loop_unref(loop);
#else
while (true)
;
#endif
return 0;
}