Skip to content
Myr

compatibility.md

This file

Path

docs/compatibility.md

172 lines · 7.4 KiB · Markdown

Last changed by

:pencil: Docs patch following PR #1791 section compatibility.encoding (#1812)TAHRI Ahmed Rbefore adoptionecbece1

History of this file

Take it

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

Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209

lines 1–2why, one step back
1# Requests Compatibility Guide
2

Update compatibility.md: mention differing query parameter handling (#1262)Bartbefore adoption4bd08be

line 3why, one step back
3HTTPX aims to be broadly compatible with the `requests` API.

Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209

lines 4–11why, one step back
4
5This 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.11

Seperate `content=...` and `data=...` parameters (#1266)Tom Christiebefore adoptionfeb404f

lines 12–30why, one step back
12## Request Content
1314For uploading raw text or binary content we prefer to use a `content` parameter,15in order to better separate this usage from the case of uploading form data.1617For example, using `content=...` to upload raw content:1819```python20# Uploading text, bytes, or a bytes iterator.21httpx.post(..., content=b"Hello, world")22```2324And using `data=...` to send form data:2526```python27# Uploading form data.28httpx.post(..., data={"message": "Hello, world"})29```30

Escalate the distinction between data=... and content=... to be stricter (#1573)Tom Christiebefore adoption397aad9

lines 31–32why, one step back
31Using the `data=<text/byte content>` will raise a deprecation warning,
32and is expected to be fully removed with the HTTPX 1.0 release.

Seperate `content=...` and `data=...` parameters (#1266)Tom Christiebefore adoptionfeb404f

line 33why, one step back
33

Document content encoding differences with Requests (#1416)Florimond Mancabefore adoption7ea6019

lines 34–37why, one step back
34## Content encoding
3536HTTPX uses `utf-8` for encoding `str` request bodies. For example, when using `content=<str>` the request body will be encoded to `utf-8` before being sent over the wire. This differs from Requests which uses `latin1`. If you need an explicit encoding, pass encoded bytes explictly, e.g. `content=<str>.encode("latin1")`.37

:pencil: Docs patch following PR #1791 section compatibility.encoding (#1812)TAHRI Ahmed Rbefore adoptionecbece1

line 38why, one step back
38For response bodies, assuming the server didn't send an explicit encoding then HTTPX will do its best to figure out an appropriate encoding. HTTPX makes a guess at the encoding to use for decoding the response using `charset_normalizer`. Fallback to that or any content with less than 32 octets will be decoded using `utf-8` with the `error="replace"` decoder strategy.

Document content encoding differences with Requests (#1416)Florimond Mancabefore adoption7ea6019

line 39why, one step back
39

Deprecate per-request cookies (#1574)Tom Christiebefore adoption6a99f6f

lines 40–59why, one step back
40## Cookies
4142If using a client instance, then cookies should always be set on the client rather than on a per-request basis.4344This usage is supported:4546```python47client = httpx.Client(cookies=...)48client.post(...)49```5051This usage is **not** supported:5253```python54client = httpx.Client()55client.post(..., cookies=...)56```5758We prefer enforcing a stricter API here because it provides clearer expectations around cookie persistence, particularly when redirects occur.59

Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209

lines 60–100why, one step back
60## Status Codes
6162In our documentation we prefer the uppercased versions, such as `codes.NOT_FOUND`, but also provide lower-cased versions for API compatibility with `requests`.6364Requests includes various synonyms for status codes that HTTPX does not support.6566## Streaming responses6768HTTPX 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.6970For example:7172```python73with request.stream("GET", "https://www.example.com") as response:74 ...75```7677Within a `stream()` block request data is made available with:7879* `.iter_bytes()` - Instead of `response.iter_content()`80* `.iter_text()` - Instead of `response.iter_content(decode_unicode=True)`81* `.iter_lines()` - Corresponding to `response.iter_lines()`82* `.iter_raw()` - Use this instead of `response.raw`83* `.read()` - Read the entire response body, making `request.text` and `response.content` available.8485## Proxy keys8687When using `httpx.Client(proxies={...})` to map to a selection of different proxies, we use full URL schemes, such as `proxies={"http://": ..., "https://": ...}`.8889This is different to the `requests` usage of `proxies={"http": ..., "https": ...}`.9091This 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.9293Also note that `requests.Session.request(...)` allows a `proxies=...` parameter, whereas `httpx.Client.request(...)` does not.9495## SSL configuration9697When 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.9899If you need more than one different SSL configuration, you should use different client instances for each SSL configuration.100

Explain REQUESTS_CA_BUNDLE migration (#1471)Quentin Pradetbefore adoption5af6ab0

lines 101–102why, one step back
101Requests supports `REQUESTS_CA_BUNDLE` which points to either a file or a directory. HTTPX supports the `SSL_CERT_FILE` (for a file) and `SSL_CERT_DIR` (for a directory) OpenSSL variables instead.
102

Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209

lines 103–128why, one step back
103## Request body on HTTP methods
104105The 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.106107If you really do need to send request data using these http methods you should use the generic `.request` function instead.108109## Checking for 4xx/5xx responses110111We 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:`.112113## Client instances114115The HTTPX equivalent of `requests.Session` is `httpx.Client`.116117```python118session = requests.Session(**kwargs)119```120121is generally equivalent to122123```python124client = httpx.Client(**kwargs)125```126127## Request instantiation128

Use relative links for interlinking markdown files in docs (#1390)Tom Christiebefore adoption0af6d9a

line 129why, one step back
129There 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.md#request-instances).

Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209

line 130why, one step back
130

Use relative links for interlinking markdown files in docs (#1390)Tom Christiebefore adoption0af6d9a

line 131why, one step back
131Besides, `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.md#client-instances).

Replace httpx.URL for str in tests (#1237)Eduardo Enriquezbefore adoptionaad8209

lines 132–135why, one step back
132
133## Mocking134135If 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).

Drop urllib3 in favor of public gist (#1182)Florimond Mancabefore adoption8fa8765

lines 136–141why, one step back
136
137## Networking layer138139`requests` defers most of its HTTP networking code to the excellent [`urllib3` library](https://urllib3.readthedocs.io/en/latest/).140141On the other hand, HTTPX uses [HTTPCore](https://github.com/encode/httpcore) as its core HTTP networking layer, which is a different project than `urllib3`.

Update compatibility.md: mention differing query parameter handling (#1262)Bartbefore adoption4bd08be

lines 142–145why, one step back
142
143## Query Parameters144145`requests` omits `params` whose values are `None` (e.g. `requests.get(..., params={"foo": None})`). This is not supported by HTTPX.

Add `response.next_request` (#1334)Tom Christiebefore adoption3f51392

line 146why, one step back
146

Fix redirect description about HEAD (#1520)laggardkernelbefore adoptionea5ffce

lines 147–151why, one step back
147## HEAD redirection
148149In `requests`, all top-level API follow redirects by default except `HEAD`.150In consideration of consistency, we make `HEAD` follow redirects by default in HTTPX.151

Add `response.next_request` (#1334)Tom Christiebefore adoption3f51392

lines 152–164why, one step back
152## Determining the next redirect request
153154When using `allow_redirects=False`, the `requests` library exposes an attribute `response.next`, which can be used to obtain the next redirect request.155156In HTTPX, this attribute is instead named `response.next_request`. For example:157158```python159client = httpx.Client()160request = client.build_request("GET", ...)161while request is not None:162 response = client.send(request, allow_redirects=False)163 request = response.next_request164```

Updates compatibility guide to address event hooks (#1436)Colton Eakinsbefore adoptione3a7b6d

lines 165–169why, one step back
165
166## Event Hooks167168`requests` allows event hooks to mutate `Request` and `Response` objects. See [examples](https://requests.readthedocs.io/en/master/user/advanced/#event-hooks) given in the documentation for `requests`.169

Escalate the distinction between data=... and content=... to be stricter (#1573)Tom Christiebefore adoption397aad9

line 170why, one step back
170In HTTPX, event hooks may access properties of requests and responses, but event hook callbacks cannot mutate the original request/response.

Updates compatibility guide to address event hooks (#1436)Colton Eakinsbefore adoptione3a7b6d

lines 171–172why, one step back
171
172If you are looking for more control, consider checking out [Custom Transports](advanced.md#custom-transports).