1.5 KiB
title, order, status
| title | order | status |
|---|---|---|
| C16. Send Requests Through a Proxy | 16 | 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
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().
cli.set_proxy("proxy.internal", 8080);
cli.set_proxy_basic_auth("user", "password");
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.
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.
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_PROXYorHTTPS_PROXYenvironment variables automatically. If you want to honor them, read them in your application and pass the values toset_proxy().