compatibility.md
This file
Path
docs/compatibility.md
83 lines · 3.8 KiB · Markdown
Last changed by
Note on differences in proxy keys vs. `requests` (#1132)Tom Christiebefore adoption3205536
History of this fileTake it
The walk stopped after 200 commits: lines older than a9f4d01 point at it.
Version 0.10.1 (#701)Tom Christiebefore adoptiona9f4d01
lines 1–15why, one step back1# Requests Compatibility Guide23HTTPX aims to be compatible with the `requests` API wherever possible.45This documentation outlines places where the API differs...67## Request URLs89Accessing `response.url` will return a `URL` instance, rather than a string.10Use `str(response.url)` if you need a string instance.1112## Status Codes1314In our documentation we prefer the uppercased versions, such as `codes.NOT_FOUND`, but also provide lower-cased versions for API compatibility with `requests`.15Fix typo on "synonyms" (#748)wynnwbefore adoptionb7c0d34
line 16why, one step back16Requests includes various synonyms for status codes that HTTPX does not support.Version 0.10.1 (#701)Tom Christiebefore adoptiona9f4d01
lines 17–24why, one step back1718## Streaming responses1920HTTPX provides a `.stream()` interface rather than using `stream=True`. This ensures that streaming responses are always properly closed outside of the stream block, and makes it visually clearer at which points streaming I/O APIs may be used with a response.2122For example:2324```pythonProposed 0.11 docs (#727)Tom Christiebefore adoption2038919
line 25why, one step back25with request.stream("GET", "https://www.example.com") as response:Version 0.10.1 (#701)Tom Christiebefore adoptiona9f4d01
lines 26–30why, one step back26 ...27```2829Within a `stream()` block request data is made available with:30Proposed 0.11 docs (#727)Tom Christiebefore adoption2038919
lines 31–35why, one step back31* `.iter_bytes()` - Instead of `response.iter_content()`32* `.iter_text()` - Instead of `response.iter_content(decode_unicode=True)`33* `.iter_lines()` - Corresponding to `response.iter_lines()`34* `.iter_raw()` - Use this instead of `response.raw`35* `.read()` - Read the entire response body, making `request.text` and `response.content` available.Version 0.10.1 (#701)Tom Christiebefore adoptiona9f4d01
line 36why, one step back36Note on differences in proxy keys vs. `requests` (#1132)Tom Christiebefore adoption3205536
lines 37–44why, one step back37## Proxy keys3839When using `httpx.Client(proxies={...})` to map to a selection of different proxies, we use full URL schemes, such as `proxies={"http://": ..., "https://": ...}`.4041This is different to the `requests` usage of `proxies={"http": ..., "https": ...}`.4243This change is for better consistency with more complex mappings, that might also include domain names, such as `proxies={"all://": ..., "all://www.example.com": None}` which maps all requests onto a proxy, except for requests to "www.example.com" which have an explicit exclusion.44Version 0.10.1 (#701)Tom Christiebefore adoptiona9f4d01
lines 45–62why, one step back45## SSL configuration4647When using a `Client` instance, the `trust_env`, `verify`, and `cert` arguments should always be passed on client instantiation, rather than passed to the request method.4849If you need more than one different SSL configuration, you should use different client instances for each SSL configuration.5051## Request body on HTTP methods5253The HTTP `GET`, `DELETE`, `HEAD`, and `OPTIONS` methods are specified as not supporting a request body. To stay in line with this, the `.get`, `.delete`, `.head` and `.options` functions do not support `files`, `data`, or `json` arguments.5455If you really do need to send request data using these http methods you should use the generic `.request` function instead.5657## Checking for 4xx/5xx responses5859We don't support `response.is_ok` since the naming is ambiguous there, and might incorrectly imply an equivalence to `response.status_code == codes.OK`. Instead we provide the `response.is_error` property. Use `if not response.is_error:` instead of `if response.is_ok:`.6061## Client instances62Proposed 0.11 docs (#727)Tom Christiebefore adoption2038919
line 63why, one step back63The HTTPX equivalent of `requests.Session` is `httpx.Client`.Version 0.10.1 (#701)Tom Christiebefore adoptiona9f4d01
lines 64–71why, one step back6465```python66session = requests.Session(**kwargs)67```6869is generally equivalent to7071```pythonProposed 0.11 docs (#727)Tom Christiebefore adoption2038919
line 72why, one step back72client = httpx.Client(**kwargs)Version 0.10.1 (#701)Tom Christiebefore adoptiona9f4d01
lines 73–74why, one step back73```74Add docs on request instances to Requests compatibility guide (#823)Srikanth Chekuribefore adoption5cf1acc
lines 75–80why, one step back75## Request instantiation7677There is no notion of [prepared requests](https://requests.readthedocs.io/en/stable/user/advanced/#prepared-requests) in HTTPX. If you need to customize request instantiation, see [Request instances](/advanced#request-instances).7879Besides, `httpx.Request()` does not support the `auth`, `timeout`, `allow_redirects`, `proxies`, `verify` and `cert` parameters. However these are available in `httpx.request`, `httpx.get`, `httpx.post` etc., as well as on [`Client` instances](/advanced#client-instances).80Version 0.10.1 (#701)Tom Christiebefore adoptiona9f4d01
lines 81–83why, one step back81## Mocking8283If you need to mock HTTPX the same way that test utilities like `responses` and `requests-mock` does for `requests`, see [RESPX](https://github.com/lundberg/respx).