1.9 KiB
1.9 KiB
Requests Compatibility Guide
HTTPX aims to be compatible with the requests API wherever possible.
This documentation outlines places where the API differs...
QuickStart
Pretty much any API mentioned in the requests QuickStart should be identical
to the API in our own documentation. The following exceptions apply:
Response.url- Returns aURLinstance, rather than a string. Usestr(response.url)if you need a string instance.httpx.codes- In our documentation we prefer the uppercased versions, such ascodes.NOT_FOUND, but also provide lower-cased versions for API compatibility withrequests.stream(). - HTTPX provides a.stream()interface rather than usingstream=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. Streaming request data is made avialable with.stream_bytes(),.stream_text(),.stream_lines(), and.stream_raw()..get,.delete,.head,.options- These methods do not supportfiles,data, orjsonarguments. Use.requestif you need to need to send data using these http methods.- We don't support
response.is_oksince the naming is ambiguous there, and might incorrectly imply an equivalence toresponse.status_code == codes.OK. Instead we provide theresponse.is_errorproperty. Useif not response.is_error:instead ofif response.is_ok:.
Advanced Usage
requests.Session
The HTTPX equivalent of requests.Session is httpx.Client.
session = requests.Session(**kwargs)
is generally equivalent to
client = httpx.Client(**kwargs)
More detailed documentation and usage of Client can be found in Advanced Usage.
Mocking
If you need to mock HTTPX the same way that test utilities like responses and requests-mock does for requests, see RESPX.