cofetch
Chainable, high-performance async HTTP client for C++ event loops
Loading...
Searching...
No Matches
example_redirects.cpp

3xx passed through by default; follow_redirects() to chase them.

3xx passed through by default; follow_redirects() to chase them.

// Redirects are opt-in. By default a 3xx comes back to you as-is;
// follow_redirects() makes curl chase the Location chain and hands you
// the final response.
#include <cofetch.h>
#include <asio.hpp>
#include <iostream>
int main() {
asio::io_context io;
cofetch::Client http(io);
const std::string bounce =
"https://postman-echo.com/redirect-to?url=https://postman-echo.com/get";
// Default: see the redirect itself.
http.async_get(bounce, [](std::error_code, const cofetch::Response& res) {
std::cout << "without follow: http " << res.http_code_ << "\n";
});
// Opt in: land on the final page (at most 30 hops by default).
http.request(bounce).follow_redirects().get(
[](std::error_code, const cofetch::Response& res) {
std::cout << "with follow: http " << res.http_code_ << "\n";
});
io.run();
}
RequestBuilder & follow_redirects(long max=30)
Definition cofetch.h:362
auto get(CompletionToken &&token)
Definition cofetch.h:372
Definition cofetch.h:266
RequestBuilder request(std::string url)
Start a fluent request chain: request(url).body(...).post(token).
Definition cofetch.h:412
auto async_get(std::string url, CompletionToken &&token)
Definition cofetch.h:324
Definition cofetch.h:111
long http_code_
Definition cofetch.h:176