Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 3 additions & 13 deletions docs/advanced/ssl.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,11 @@ ctx.load_cert_chain(certfile="path/to/client.pem") # Optionally also keyfile or
client = httpx.Client(verify=ctx)
```

### Working with `SSL_CERT_FILE` and `SSL_CERT_DIR`
### Providing CA from environment

Unlike `requests`, the `httpx` package does not automatically pull in [the environment variables `SSL_CERT_FILE` or `SSL_CERT_DIR`](https://www.openssl.org/docs/manmaster/man3/SSL_CTX_set_default_verify_paths.html). If you want to use these they need to be enabled explicitly.
`httpx` package automatically pulls in [the environment variables `SSL_CERT_FILE` or `SSL_CERT_DIR`](https://www.openssl.org/docs/manmaster/man3/SSL_CTX_set_default_verify_paths.html) untill trust_env is `True`.

For example...

```python
# Use `SSL_CERT_FILE` or `SSL_CERT_DIR` if configured.
# Otherwise default to certifi.
ctx = ssl.create_default_context(
cafile=os.environ.get("SSL_CERT_FILE", certifi.where()),
capath=os.environ.get("SSL_CERT_DIR"),
)
client = httpx.Client(verify=ctx)
```
Alternatively you can use `HTTPX_CA_BUNDLE` env which acts as `SSL_CERT_FILE`.

### Making HTTPS requests to a local server

Expand Down
6 changes: 6 additions & 0 deletions docs/environment_variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,9 @@ python -c "import httpx; httpx.get('http://example.com')"
python -c "import httpx; httpx.get('http://127.0.0.1:5000/my-api')"
python -c "import httpx; httpx.get('https://www.python-httpx.org')"
```


## TLS

### `HTTPX_CA_BUNDLE`
Overrides default `certifi` trust store
2 changes: 2 additions & 0 deletions httpx/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ def create_ssl_context(
if verify is True:
if trust_env and os.environ.get("SSL_CERT_FILE"): # pragma: nocover
ctx = ssl.create_default_context(cafile=os.environ["SSL_CERT_FILE"])
elif trust_env and os.environ.get("HTTPX_CA_BUNDLE"): # pragma: nocover
ctx = ssl.create_default_context(cafile=os.environ["HTTPX_CA_BUNDLE"])
elif trust_env and os.environ.get("SSL_CERT_DIR"): # pragma: nocover
ctx = ssl.create_default_context(capath=os.environ["SSL_CERT_DIR"])
else:
Expand Down