Kurlyk
Loading...
Searching...
No Matches
redirect_handling_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
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
17int main() {
18 kurlyk::init(true);
19 kurlyk::HttpClient client("https://httpbin.org");
20
21 // Set client settings
22 client.set_user_agent("KurlykClient/1.0");
23 client.set_timeout(15); // Request timeout (seconds)
24 client.set_connect_timeout(5); // Connection timeout (seconds)
25 client.set_retry_attempts(3, 1000); // 3 retry attempts, 1-second delay between retries
26 client.set_rate_limit(5, 1000); // Rate limit: 5 requests per second
27 // Configure the maximum number of redirects
28 int redirect_count = 15; // Allow up to 15 redirects
29 client.set_max_redirects(redirect_count);
30
31 // Redirect handling - GET request with redirections
32 KURLYK_PRINT << "Sending GET request to /absolute-redirect/" << redirect_count << "..." << std::endl;
33 client.get("/absolute-redirect/" + std::to_string(redirect_count), kurlyk::QueryParams(), kurlyk::Headers(),
34 [](const kurlyk::HttpResponsePtr response) {
35 print_response(response);
36 });
37
38 KURLYK_PRINT << "Press Enter to exit..." << std::endl;
39 std::cin.get();
40 client.cancel_requests();
42 return 0;
43}
44
Concrete HTTP client for making requests to a specific host.
void set_rate_limit(long requests_per_period, long period_ms, RateLimitType type=RateLimitType::RL_GENERAL, bool sequential=false)
Creates and assigns an owned rate limit for future requests.
void set_max_redirects(long max_redirects)
Sets the maximum number of redirects for the client.
void set_connect_timeout(long connect_timeout)
Sets the connection timeout duration.
void set_retry_attempts(long retry_attempts, long retry_delay_ms)
Sets retry attempts and delay between retries for HTTP requests.
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 requests associated with this client and waits for cancellation callbacks.
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.
utils::CaseInsensitiveMultimap QueryParams
Alias for query parameters in HTTP requests, stored case-insensitively.
#define KURLYK_PRINT
void print_response(const kurlyk::HttpResponsePtr &response)