Kurlyk
Loading...
Searching...
No Matches
http_client_future_example.cpp
Go to the documentation of this file.
1#include <iostream>
2#include <kurlyk.hpp>
3#include <future>
4
5int main() {
6 kurlyk::HttpClient client("https://httpbin.org");
7
8 KURLYK_PRINT << "Sending GET request using HttpClient method..." << std::endl;
9 std::future<kurlyk::HttpResponsePtr> future_response = client.get("/ip", kurlyk::QueryParams(), kurlyk::Headers());
10
11 kurlyk::HttpResponsePtr response = future_response.get();
13 << "Response from HttpClient method:" << std::endl
14 << "ready: " << response->ready << std::endl
15 << "content: " << response->content << std::endl
16 << "error_code: " << response->error_code << std::endl
17 << "status_code: " << response->status_code << std::endl
18 << "----------------------------------------" << std::endl;
19
20 KURLYK_PRINT << "Sending GET request using standalone function..." << std::endl;
21# if __cplusplus >= 201703L
22 auto [request_id, future] = kurlyk::http_get("https://httpbin.org/ip", kurlyk::QueryParams(), kurlyk::Headers());
23# else
24 auto func_result = kurlyk::http_get("https://httpbin.org/ip", kurlyk::QueryParams(), kurlyk::Headers());
25 uint64_t request_id = func_result.first;
26 auto future = std::move(func_result.second);
27# endif
28
29 kurlyk::HttpResponsePtr response_func = future.get();
31 << "Response from standalone function:" << std::endl
32 << "Request ID: " << request_id << std::endl
33 << "Ready: " << std::boolalpha << response_func->ready << std::endl
34 << "Content: " << response_func->content << std::endl
35 << "Error Code: " << response_func->error_code.message() << std::endl
36 << "Status Code: " << response_func->status_code << std::endl
37 << "----------------------------------------" << std::endl;
38
39 KURLYK_PRINT << "Press Enter to exit..." << std::endl;
40 std::cin.get();
41
43 return 0;
44}
A client class for making HTTP 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.
std::unique_ptr< HttpResponse > HttpResponsePtr
A unique pointer to an HttpResponse object for memory management.
void deinit()
Deinitializes the Kurlyk library, stopping the network worker and releasing resources.
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:260
utils::CaseInsensitiveMultimap QueryParams
Alias for query parameters in HTTP requests, stored case-insensitively.
#define KURLYK_PRINT