Skip to content
Myr

compatibility.md

This file

Path

docs/compatibility.md

69 lines · 2.7 KiB · Markdown

Last changed by

Fix typo on "URLs" (#630)Tom Christiebefore adoption77da6db

History of this file

Take it

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

Merge pull request #314 from dobrovolsky/drop_npnTom Christiebefore adoptioncbe7616

lines 1–6why, 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...6

Fix typo on "URLs" (#630)Tom Christiebefore adoption77da6db

line 7why, one step back
7## Request URLs

Merge pull request #314 from dobrovolsky/drop_npnTom Christiebefore adoptioncbe7616

line 8why, one step back
8

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

lines 9–10why, one step back
9Accessing `response.url` will return a `URL` instance, rather than a string.
10Use `str(response.url)` if you need a string instance.

Merge pull request #314 from dobrovolsky/drop_npnTom Christiebefore adoptioncbe7616

line 11why, one step back
11

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

line 12why, one step back
12## Status Codes

Merge pull request #314 from dobrovolsky/drop_npnTom Christiebefore adoptioncbe7616

line 13why, one step back
13

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

line 14why, one step back
14In our documentation we prefer the uppercased versions, such as `codes.NOT_FOUND`, but also provide lower-cased versions for API compatibility with `requests`.

Merge pull request #314 from dobrovolsky/drop_npnTom Christiebefore adoptioncbe7616

line 15why, one step back
15

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

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

Drop sync (#544)Tom Christiebefore adoption206c537

line 17why, one step back
17

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

lines 18–30why, one step back
18## Streaming responses
1920HTTPX 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```python25async with request.stream("GET", "https://www.example.com") as response:26 ...27```2829Within a `stream()` block request data is made available with:30

Add aiter methods on response (#610)Tom Christiebefore adoptionec40d04

lines 31–34why, one step back
31* `.aiter_bytes()` - Instead of `response.iter_content()`
32* `.aiter_text()` - Instead of `response.iter_content(decode_unicode=True)`33* `.aiter_lines()` - Instead of `response.iter_lines()`34* `.aiter_raw()` - Use this instead of `response.raw`

Warn if cert / verify / trust_env are passed to client.request() (#597)Tom Christiebefore adoption1e23855

lines 35–54why, one step back
35* `.read()` - Read the entire response body, making `request.text` and `response.content` available.
3637## 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

Add documentation for requests.Session compatibility (#449)Jt Miclatbefore adoption31730e7

lines 55–60why, one step back
55The HTTPX equivalent of `requests.Session` is `httpx.Client`.
5657```python58session = requests.Session(**kwargs)59```60

Drop sync (#544)Tom Christiebefore adoption206c537

line 61why, one step back
61is generally equivalent to

Add documentation for requests.Session compatibility (#449)Jt Miclatbefore adoption31730e7

lines 62–63why, one step back
62
63```python

Drop sync (#544)Tom Christiebefore adoption206c537

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

Add documentation for requests.Session compatibility (#449)Jt Miclatbefore adoption31730e7

line 65why, one step back
65```

Document mocking compatibility (#537)Jonas Lundbergbefore adoption2dbcaf8

lines 66–69why, one step back
66
67## Mocking6869If 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).