Kurlyk
Loading...
Searching...
No Matches
nested_http_requests_example.cpp
Go to the documentation of this file.
1#define KURLYK_AUTO_INIT 0
2#include <kurlyk.hpp>
3#include <iostream>
4
5// Thread-safe logging of response details
8 << "Response received:" << std::endl
9 << "Ready: " << response->ready << std::endl
10 << "Content: " << response->content << std::endl
11 << "Error Code: " << response->error_code << std::endl
12 << "Status Code: " << response->status_code << std::endl
13 << "----------------------------------------" << std::endl;
14}
15
16int main() {
17 kurlyk::init(true);
18
19 // Initialize the HTTP client
20 kurlyk::HttpClient client("https://httpbin.org");
21 client.set_user_agent("KurlykClient/1.0");
22 client.set_verbose(true);
23
24 // Send the first GET request
25 KURLYK_PRINT << "Sending the first GET request..." << std::endl;
26 client.get("/ip", kurlyk::QueryParams(), kurlyk::Headers(),
27 [&client](const kurlyk::HttpResponsePtr response) {
28 print_response(response);
29
30 if (response->ready && response->status_code == 200) {
31 KURLYK_PRINT << "First request succeeded. Sending the second request..." << std::endl;
32
33 // Send a second GET request within the callback of the first request
34 client.get("/user-agent", kurlyk::QueryParams(), kurlyk::Headers(),
35 [](const kurlyk::HttpResponsePtr response) {
36 KURLYK_PRINT << "Second request completed." << std::endl;
37 print_response(response);
38 });
39 } else {
40 KURLYK_PRINT << "First request failed. Not sending the second request." << std::endl;
41 }
42 });
43
44 KURLYK_PRINT << "Press Enter to exit..." << std::endl;
45 std::cin.get();
46 client.cancel_requests();
48 return 0;
49}
Concrete HTTP client for making requests to a specific host.
void set_verbose(bool verbose)
Enables or disables verbose output.
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.
void print_response(const kurlyk::HttpResponsePtr &response)
#define KURLYK_PRINT