mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-04-11 19:28:30 +00:00
Add Cookbook C01-C19 (draft)
This commit is contained in:
52
docs-src/pages/en/cookbook/c16-proxy.md
Normal file
52
docs-src/pages/en/cookbook/c16-proxy.md
Normal file
@@ -0,0 +1,52 @@
|
||||
---
|
||||
title: "C16. Send Requests Through a Proxy"
|
||||
order: 16
|
||||
status: "draft"
|
||||
---
|
||||
|
||||
To route traffic through a corporate network or a specific path, send requests via an HTTP proxy. Just pass the proxy host and port to `set_proxy()`.
|
||||
|
||||
## Basic usage
|
||||
|
||||
```cpp
|
||||
httplib::Client cli("https://api.example.com");
|
||||
cli.set_proxy("proxy.internal", 8080);
|
||||
|
||||
auto res = cli.Get("/users");
|
||||
```
|
||||
|
||||
The request goes through the proxy. For HTTPS, the client uses the CONNECT method to tunnel through — no extra setup required.
|
||||
|
||||
## Proxy authentication
|
||||
|
||||
If the proxy itself requires authentication, use `set_proxy_basic_auth()` or `set_proxy_bearer_token_auth()`.
|
||||
|
||||
```cpp
|
||||
cli.set_proxy("proxy.internal", 8080);
|
||||
cli.set_proxy_basic_auth("user", "password");
|
||||
```
|
||||
|
||||
```cpp
|
||||
cli.set_proxy_bearer_token_auth("token");
|
||||
```
|
||||
|
||||
If cpp-httplib is built with OpenSSL (or another TLS backend), you can also use Digest authentication for the proxy.
|
||||
|
||||
```cpp
|
||||
cli.set_proxy_digest_auth("user", "password");
|
||||
```
|
||||
|
||||
## Combine with end-server authentication
|
||||
|
||||
Proxy authentication is separate from authenticating to the end server (C05, C06). When both are needed, set both.
|
||||
|
||||
```cpp
|
||||
cli.set_proxy("proxy.internal", 8080);
|
||||
cli.set_proxy_basic_auth("proxy-user", "proxy-pass");
|
||||
|
||||
cli.set_bearer_token_auth("api-token"); // for the end server
|
||||
```
|
||||
|
||||
`Proxy-Authorization` is sent to the proxy, `Authorization` to the end server.
|
||||
|
||||
> **Note:** cpp-httplib does not read `HTTP_PROXY` or `HTTPS_PROXY` environment variables automatically. If you want to honor them, read them in your application and pass the values to `set_proxy()`.
|
||||
Reference in New Issue
Block a user