Kurlyk
Toggle main menu visibility
Loading...
Searching...
No Matches
http_client_future_example.cpp
Go to the documentation of this file.
1
#include <iostream>
2
#define KURLYK_AUTO_INIT 0
3
#include <
kurlyk.hpp
>
4
#include <future>
5
6
int
main
() {
7
kurlyk::init
(
true
);
8
kurlyk::HttpClient
client(
"https://httpbin.org"
);
9
10
KURLYK_PRINT
<<
"Sending GET request using HttpClient method..."
<< std::endl;
11
std::future<kurlyk::HttpResponsePtr> future_response = client.
get
(
"/ip"
,
kurlyk::QueryParams
(),
kurlyk::Headers
());
12
13
kurlyk::HttpResponsePtr
response = future_response.get();
14
KURLYK_PRINT
15
<<
"Response from HttpClient method:"
<< std::endl
16
<<
"ready: "
<< response->ready << std::endl
17
<<
"content: "
<< response->content << std::endl
18
<<
"error_code: "
<< response->error_code << std::endl
19
<<
"status_code: "
<< response->status_code << std::endl
20
<<
"----------------------------------------"
<< std::endl;
21
22
KURLYK_PRINT
<<
"Sending GET request using standalone function..."
<< std::endl;
23
# if __cplusplus >= 201703L
24
auto
[request_id, future] =
kurlyk::http_get
(
"https://httpbin.org/ip"
,
kurlyk::QueryParams
(),
kurlyk::Headers
());
25
# else
26
auto
func_result =
kurlyk::http_get
(
"https://httpbin.org/ip"
,
kurlyk::QueryParams
(),
kurlyk::Headers
());
27
uint64_t request_id = func_result.first;
28
auto
future = std::move(func_result.second);
29
# endif
30
31
kurlyk::HttpResponsePtr
response_func = future.get();
32
KURLYK_PRINT
33
<<
"Response from standalone function:"
<< std::endl
34
<<
"Request ID: "
<< request_id << std::endl
35
<<
"Ready: "
<< std::boolalpha << response_func->ready << std::endl
36
<<
"Content: "
<< response_func->content << std::endl
37
<<
"Error Code: "
<< response_func->error_code.message() << std::endl
38
<<
"Status Code: "
<< response_func->status_code << std::endl
39
<<
"----------------------------------------"
<< std::endl;
40
41
KURLYK_PRINT
<<
"Press Enter to exit..."
<< std::endl;
42
std::cin.get();
43
44
kurlyk::deinit
();
45
return
0;
46
}
kurlyk::HttpClient
Concrete HTTP client for making requests to a specific host.
Definition
HttpClient.hpp:17
kurlyk::HttpClient::get
bool get(const std::string &path, const QueryParams &query, const Headers &headers, HttpResponseCallback callback)
Sends a GET request.
Definition
HttpClient.hpp:681
main
int main()
Definition
http_client_future_example.cpp:6
kurlyk.hpp
Main header file for the Kurlyk library, providing HTTP and WebSocket support.
kurlyk::init
void init(const bool use_async=true)
Initializes the Kurlyk library, setting up necessary managers and the network worker.
Definition
runtime.hpp:13
kurlyk::HttpResponsePtr
std::unique_ptr< HttpResponse > HttpResponsePtr
Owning pointer to an HTTP response.
Definition
HttpResponse.hpp:36
kurlyk::deinit
void deinit()
Deinitializes the Kurlyk library, stopping async processing or cleaning up synchronous state.
Definition
runtime.hpp:26
kurlyk::Headers
utils::CaseInsensitiveMultimap Headers
Alias for HTTP headers, providing a case-insensitive unordered multimap.
Definition
CaseInsensitiveMultimap.hpp:62
kurlyk::http_get
uint64_t http_get(const std::string &url, const QueryParams &query, const Headers &headers, HttpResponseCallback callback)
Sends an asynchronous HTTP GET request with a callback.
Definition
utils.hpp:421
kurlyk::QueryParams
utils::CaseInsensitiveMultimap QueryParams
Alias for query parameters in HTTP requests, stored case-insensitively.
Definition
CaseInsensitiveMultimap.hpp:65
KURLYK_PRINT
#define KURLYK_PRINT
Definition
print_utils.hpp:8
examples
http_client_future_example.cpp
Generated by
1.17.0