Kurlyk
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
6int 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();
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();
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
45 return 0;
46}
Concrete HTTP client for making requests to a specific host.
bool get(const std::string &path, const QueryParams &query, const Headers &headers, HttpResponseCallback callback)
Sends a GET request.
Main header file for the Kurlyk library, providing HTTP and WebSocket support.
void init(const bool use_async=true)
Initializes the Kurlyk library, setting up necessary managers and the network worker.
Definition runtime.hpp:13
std::unique_ptr< HttpResponse > HttpResponsePtr
Owning pointer to an HTTP response.
void deinit()
Deinitializes the Kurlyk library, stopping async processing or cleaning up synchronous state.
Definition runtime.hpp:26
utils::CaseInsensitiveMultimap Headers
Alias for HTTP headers, providing a case-insensitive unordered multimap.
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
utils::CaseInsensitiveMultimap QueryParams
Alias for query parameters in HTTP requests, stored case-insensitively.
#define KURLYK_PRINT