mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-10-21 11:39:48 +00:00
Bring the READMEs and the docs pages (en and ja) in line with the current header: - Fix the client sample that does not compile, the Transfer-Encoding statement for default headers, the verifier callback comment, the thread pool spawn rule, the sanitize_filename description, and the inverted Accept-Encoding sentence. - Describe recent behaviour: TLS errors behind Read/Write failures, Host validation, Connection: close for a body without length, content_reader rules, refused CONNECT, WebSocketClient proxies, CPPHTTPLIB_INTERIM_RESPONSE_MAX_COUNT, the SSE event size limit, and the payload limit of the Stream API. - Complete the API references of WebSocket, WebSocketClient and the stream:: helpers, and the SSE reconnect rules. - Fix cookbook pages: keychain loading on every backend, received Close status, a multipart sample that trusted the client's file name, an echo sample that sent binary as text, SNI, pre-request handler scope, stop() with WebSocket handlers, and the thread pool notes. - Restore the mutex in the LLM app's Chapter 5 code.
41 lines
1.7 KiB
Markdown
41 lines
1.7 KiB
Markdown
---
|
|
title: "C04. Follow Redirects"
|
|
order: 4
|
|
status: "draft"
|
|
---
|
|
|
|
By default, cpp-httplib does not follow HTTP redirects (3xx). If the server returns `302 Found`, you'll get it as a response with status code 302 — nothing more.
|
|
|
|
To follow redirects automatically, call `set_follow_location(true)`.
|
|
|
|
## Follow redirects
|
|
|
|
```cpp
|
|
httplib::Client cli("http://example.com");
|
|
cli.set_follow_location(true);
|
|
|
|
auto res = cli.Get("/old-path");
|
|
if (res && res->status == 200) {
|
|
std::cout << res->body << std::endl;
|
|
}
|
|
```
|
|
|
|
With `set_follow_location(true)`, the client reads the `Location` header and reissues the request to the new URL automatically. The final response ends up in `res`.
|
|
|
|
## Redirects from HTTP to HTTPS
|
|
|
|
```cpp
|
|
httplib::Client cli("http://example.com");
|
|
cli.set_follow_location(true);
|
|
|
|
auto res = cli.Get("/");
|
|
```
|
|
|
|
Many sites redirect HTTP traffic to HTTPS. With `set_follow_location(true)` on, this case is handled transparently — the client follows redirects even when the scheme or host changes.
|
|
|
|
When a redirect goes to another origin (a different scheme, host, or port), the client drops the `Authorization`, `Cookie`, and `Proxy-Authorization` headers, along with the credentials set with `set_basic_auth()` and the like, and doesn't present the client certificate. Other headers, default ones included, go along, and so do the timeouts, the payload limit, and the TLS verification settings.
|
|
|
|
> **Warning:** To follow redirects to HTTPS, you need to build cpp-httplib with OpenSSL (or another TLS backend). Without TLS support, redirects to HTTPS will fail.
|
|
|
|
> **Note:** Following redirects adds to the total request time. See [C12. Set timeouts](../c12-timeouts) for timeout configuration.
|