Skip to content
Myr

compatibility.md

This file

Path

docs/compatibility.md

75 lines · 3.2 KiB · Markdown

Last changed by

Add docs on request instances to Requests compatibility guide (#823)Srikanth Chekuribefore adoption5cf1acc

History of this file

Take it

The walk stopped after 200 commits: lines older than e9ebd1d point at it.

Drop per-request `cert`, `verify`, and `trust_env` (#617)Florimond Mancabefore adoptione9ebd1d

lines 1–15why, one step back
1# Requests Compatibility Guide
23HTTPX 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`.15

Fix typo on "synonyms" (#748)wynnwbefore adoptionb7c0d34

line 16why, one step back
16Requests includes various synonyms for status codes that HTTPX does not support.

Drop per-request `cert`, `verify`, and `trust_env` (#617)Florimond Mancabefore adoptione9ebd1d

lines 17–24why, one step back
17
18## 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```python

Proposed 0.11 docs (#727)Tom Christiebefore adoption2038919

line 25why, one step back
25with request.stream("GET", "https://www.example.com") as response:

Drop per-request `cert`, `verify`, and `trust_env` (#617)Florimond Mancabefore adoptione9ebd1d

lines 26–30why, one step back
26 ...
27```2829Within a `stream()` block request data is made available with:30

Proposed 0.11 docs (#727)Tom Christiebefore adoption2038919

lines 31–35why, one step back
31* `.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.

Drop per-request `cert`, `verify`, and `trust_env` (#617)Florimond Mancabefore adoptione9ebd1d

lines 36–54why, one step back
36
37## SSL configuration3839When 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.4041If you need more than one different SSL configuration, you should use different client instances for each SSL configuration.4243## Request body on HTTP methods4445The 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.4647If you really do need to send request data using these http methods you should use the generic `.request` function instead.4849## Checking for 4xx/5xx responses5051We 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:`.5253## Client instances54

Proposed 0.11 docs (#727)Tom Christiebefore adoption2038919

line 55why, one step back
55The HTTPX equivalent of `requests.Session` is `httpx.Client`.

Drop per-request `cert`, `verify`, and `trust_env` (#617)Florimond Mancabefore adoptione9ebd1d

lines 56–63why, one step back
56
57```python58session = requests.Session(**kwargs)59```6061is generally equivalent to6263```python

Proposed 0.11 docs (#727)Tom Christiebefore adoption2038919

line 64why, one step back
64client = httpx.Client(**kwargs)

Drop per-request `cert`, `verify`, and `trust_env` (#617)Florimond Mancabefore adoptione9ebd1d

lines 65–66why, one step back
65```
66

Add docs on request instances to Requests compatibility guide (#823)Srikanth Chekuribefore adoption5cf1acc

lines 67–72why, one step back
67## Request instantiation
6869There 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).7071Besides, `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).72

Drop per-request `cert`, `verify`, and `trust_env` (#617)Florimond Mancabefore adoptione9ebd1d

lines 73–75why, one step back
73## Mocking
7475If 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).