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