compatibility.md
This file
Path
docs/compatibility.md
117 lines · 5.1 KiB · Markdown
Last changed by
Seperate `content=...` and `data=...` parameters (#1266)Tom Christiebefore adoptionfeb404f
History of this fileTake it
The walk stopped after 200 commits: lines older than 8710079 point at it.
asgi: Send http.disconnect message on receive() after response complete (#909)Jamie Hewlandbefore adoption8710079
lines 1–2why, one step back1# Requests Compatibility Guide2Update compatibility.md: mention differing query parameter handling (#1262)Bartbefore adoption4bd08be
line 3why, one step back3HTTPX aims to be broadly compatible with the `requests` API.asgi: Send http.disconnect message on receive() after response complete (#909)Jamie Hewlandbefore adoption8710079
lines 4–11why, one step back45This 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.11Seperate `content=...` and `data=...` parameters (#1266)Tom Christiebefore adoptionfeb404f
lines 12–33why, one step back12## Request Content1314For 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```3031If you're using a type checking tool such as `mypy`, you'll see warnings issues if using test/byte content with the `data` argument.32However, for compatibility reasons with `requests`, we do still handle the case where `data=...` is used with raw binary and text contents.33asgi: Send http.disconnect message on receive() after response complete (#909)Jamie Hewlandbefore adoption8710079
lines 34–58why, one step back34## Status Codes3536In our documentation we prefer the uppercased versions, such as `codes.NOT_FOUND`, but also provide lower-cased versions for API compatibility with `requests`.3738Requests includes various synonyms for status codes that HTTPX does not support.3940## Streaming responses4142HTTPX 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.4344For example:4546```python47with request.stream("GET", "https://www.example.com") as response:48 ...49```5051Within a `stream()` block request data is made available with:5253* `.iter_bytes()` - Instead of `response.iter_content()`54* `.iter_text()` - Instead of `response.iter_content(decode_unicode=True)`55* `.iter_lines()` - Corresponding to `response.iter_lines()`56* `.iter_raw()` - Use this instead of `response.raw`57* `.read()` - Read the entire response body, making `request.text` and `response.content` available.58Note on differences in proxy keys vs. `requests` (#1132)Tom Christiebefore adoption3205536
lines 59–66why, one step back59## Proxy keys6061When using `httpx.Client(proxies={...})` to map to a selection of different proxies, we use full URL schemes, such as `proxies={"http://": ..., "https://": ...}`.6263This is different to the `requests` usage of `proxies={"http": ..., "https": ...}`.6465This 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.66Add `proxies` parameter to top-level API functions (#1198)Joebefore adoption84ca201
lines 67–68why, one step back67Also note that `requests.Session.request(...)` allows a `proxies=...` parameter, whereas `httpx.Client.request(...)` does not.68asgi: Send http.disconnect message on receive() after response complete (#909)Jamie Hewlandbefore adoption8710079
lines 69–107why, one step back69## SSL configuration7071When 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.7273If you need more than one different SSL configuration, you should use different client instances for each SSL configuration.7475## Request body on HTTP methods7677The 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.7879If you really do need to send request data using these http methods you should use the generic `.request` function instead.8081## Checking for 4xx/5xx responses8283We 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:`.8485## Client instances8687The HTTPX equivalent of `requests.Session` is `httpx.Client`.8889```python90session = requests.Session(**kwargs)91```9293is generally equivalent to9495```python96client = httpx.Client(**kwargs)97```9899## Request instantiation100101There 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).102103Besides, `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).104105## Mocking106107If 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 108–113why, one step back108109## Networking layer110111`requests` defers most of its HTTP networking code to the excellent [`urllib3` library](https://urllib3.readthedocs.io/en/latest/).112113On 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 114–117why, one step back114115## Query Parameters116117`requests` omits `params` whose values are `None` (e.g. `requests.get(..., params={"foo": None})`). This is not supported by HTTPX.