Skip to content
Myr

Add docs on SSL certificates (#510)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* Add docs on SSL certificates * Update docs on verify and cert params * Tweak wording * Tweak wording about localhost * Remove advanced warning * Rephrase introduction of local HTTPS section

authored by Florimond Manca · committed by GitHub · parent 08069e9 · Browse files · 3 files+52 added−8 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+52 added−8 removed

  • other:+52 added−8 removed

3 files+52 added−8 removed

modified:docs/advanced.md+35 added
360 unmodified lines
361 },362 ...363}364```365366## SSL certificates367368When making a request over HTTPS, HTTPX needs to verify the identity of the requested host. To do this, it uses a bundle of SSL certificates (a.k.a. CA bundle) delivered by a trusted certificate authority (CA).369370### Default CA bundle371372By default, HTTPX uses the CA bundle provided by [Certifi](https://pypi.org/project/certifi/). This is what you want in most cases, even though some advanced situations may require you to use a different set of certificates.373374### Using a custom CA bundle375376If you'd like to use a custom CA bundle, you can use the `verify` parameter that is available on the high-level API as well as clients. For example:377378```python379import httpx380381r = httpx.get("https://example.org", verify="path/to/client.pem")382```383384### Making HTTPS requests to a local server385386When making requests to local servers, such as a development server running on `localhost`, you will typically be using unencrypted HTTP connections.387388If you do need to make HTTPS connections to a local server, for example to test an HTTPS-only service, you will need to create and use your own certificates. Here's one way to do it:3893901. Use [trustme-cli](https://github.com/sethmlarson/trustme-cli/) to generate a pair of server key/cert files, and a client cert file.3911. Pass the server key/cert files when starting your local server. (This depends on the particular web server you're using. For example, [Uvicorn](https://www.uvicorn.org) provides the `--ssl-keyfile` and `--ssl-certfile` options.)3921. Tell HTTPX to use the certificates stored in `client.pem`:393394```python395>>> import httpx396>>> r = httpx.get("https://localhost:8000", verify="/tmp/client.pem")397>>> r398Response <200 OK>399```
modified:httpx/api.py+6 added-3 removed
25 unmodified lines
26 cookies: CookieTypes = None,27 auth: AuthTypes = None,28 timeout: TimeoutTypes = None,29 allow_redirects: bool = True,30 cert: CertTypes = None,30 verify: VerifyTypes = True,31 cert: CertTypes = None,32 stream: bool = False,33 trust_env: bool = None,34) -> Response:35 """
20 unmodified lines
56 request.57 * **timeout** - *(optional)* The timeout configuration to use when sending58 the request.59 * **allow_redirects** - *(optional)* Enables or disables HTTP redirects.60 * **verify** - *(optional)* SSL certificates (a.k.a CA bundle) used to61 verify the identity of requested hosts. Either `True` (default CA bundle),62 a path to an SSL certificate file, or `False` (disable verification).63 * **cert** - *(optional)* An SSL certificate used by the requested host60 * **cert** - *(optional)* Either a path to an SSL certificate file, or64 to authenticate the client. Either a path to an SSL certificate file, or65 two-tuple of (certificate file, key file), or a three-tuple of (certificate66 file, key file, password).63 * **verify** - *(optional)* Enables or disables SSL verification.67 * **trust_env** - *(optional)* Enables or disables usage of environment68 variables for configuration.69 * **proxies** - *(optional)* A dictionary mapping HTTP protocols to proxy70 URLs.
307 unmodified lines
modified:httpx/client.py+11 added-5 removed
692 unmodified lines
693 * **headers** - *(optional)* Dictionary of HTTP headers to include when694 sending requests.695 * **cookies** - *(optional)* Dictionary of Cookie items to include when696 sending requests.697 * **verify** - *(optional)* Enables or disables SSL verification.698 * **cert** - *(optional)* Either a path to an SSL certificate file, or697 * **verify** - *(optional)* SSL certificates (a.k.a CA bundle) used to698 verify the identity of requested hosts. Either `True` (default CA bundle),699 a path to an SSL certificate file, or `False` (disable verification).700 * **cert** - *(optional)* An SSL certificate used by the requested host701 to authenticate the client. Either a path to an SSL certificate file, or702 two-tuple of (certificate file, key file), or a three-tuple of (certificate703 file, key file, password).704 * **http_versions** - *(optional)* A list of strings of HTTP protocol705 versions to use when sending requests. eg. `http_versions=["HTTP/1.1"]`
72 unmodified lines
778 cookies: CookieTypes = None,779 stream: bool = False,780 auth: AuthTypes = None,781 allow_redirects: bool = True,779 cert: CertTypes = None,782 verify: VerifyTypes = None,783 cert: CertTypes = None,784 timeout: TimeoutTypes = None,785 trust_env: bool = None,786 ) -> Response:787 """
19 unmodified lines
807 * **stream** - *(optional)* Enable/disable streaming responses.808 * **auth** - *(optional)* An authentication class to use when sending the809 request.810 * **allow_redirects** - *(optional)* Enables or disables HTTP redirects.811 * **verify** - *(optional)* SSL certificates (a.k.a CA bundle) used to812 verify the identity of requested hosts. Either `True` (default CA bundle),813 a path to an SSL certificate file, or `False` (disable verification).814 * **cert** - *(optional)* An SSL certificate used by the requested host808 * **cert** - *(optional)* Either a path to an SSL certificate file, or815 to authenticate the client. Either a path to an SSL certificate file, or816 two-tuple of (certificate file, key file), or a three-tuple of (certificate817 file, key file, password).811 * **verify** - *(optional)* Enables or disables SSL verification.818 * **timeout** - *(optional)* The timeout configuration to use when sending819 the request.820 * **trust_env** - *(optional)* Enables or disables usage of environment821 variables for configuration.
424 unmodified lines