Kurlyk
Loading...
Searching...
No Matches
interrupted_requests_example.cpp
Go to the documentation of this file.
1#include <iostream>
2#include <kurlyk.hpp>
3#include <vector>
4
7 << "ready: " << response->ready << std::endl
8 << "response: " << std::endl
9 << response->content << std::endl
10 << "error_code: " << response->error_code << std::endl
11 << "status_code: " << response->status_code << std::endl
12 << "retry_attempt: " << response->retry_attempt << std::endl
13 << "----------------------------------------" << std::endl;
14}
15
16int main() {
17 kurlyk::init(true);
18 kurlyk::HttpClient client("https://httpbin.org");
19
20 // Set up client settings
21 client.set_user_agent("KurlykClient/1.0");
22 client.set_timeout(10);
23 client.set_connect_timeout(5);
24
25 std::vector<std::string> paths = {"/delay/3", "/delay/5", "/delay/7"};
26
27 // Send multiple GET requests
28 KURLYK_PRINT << "Sending multiple GET requests and immediately calling deinit()..." << std::endl;
29 for (const auto& path : paths) {
30 client.get(path, kurlyk::QueryParams(), kurlyk::Headers(),
31 [](const kurlyk::HttpResponsePtr response) {
32 print_response(response);
33 });
34 }
35
36 // Immediately deinitialize the library to interrupt requests
37 client.cancel_requests();
39 KURLYK_PRINT << "Kurlyk library deinitialized. Waiting to see callback responses..." << std::endl;
40 return 0;
41}
A client class for making HTTP requests to a specific host.
void set_connect_timeout(long connect_timeout)
Sets the connection timeout duration.
void set_timeout(long timeout)
Sets the timeout duration for HTTP requests.
void set_user_agent(const std::string &user_agent)
Sets the User-Agent header.
bool get(const std::string &path, const QueryParams &query, const Headers &headers, HttpResponseCallback callback)
Sends a GET request.
void cancel_requests()
Cancels the active request associated with this client and waits for its completion.
void print_response(const kurlyk::HttpResponsePtr &response)
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
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.
utils::CaseInsensitiveMultimap QueryParams
Alias for query parameters in HTTP requests, stored case-insensitively.
#define KURLYK_PRINT