lexiforest / curl_cffi

Python binding for curl-impersonate fork via cffi. A http client that can impersonate browser tls/ja3/http2 fingerprints.
https://curl-cffi.readthedocs.io/
MIT License
2.52k stars 266 forks source link

Add `base_url` to `BaseSession` #279

Closed lebr0nli closed 8 months ago

lebr0nli commented 8 months ago

Resolves #181


Here're some example usages of this new parameter:

>>> requests.Session(base_url="http://www.example.com/").get("/test").url
'http://www.example.com/test'
>>> requests.Session(base_url="http://www.example.com/a/b").get("c").url
'http://www.example.com/a/c'
>>> requests.Session(base_url="http://www.example.com/a/b/").get("c").url
'http://www.example.com/a/b/c'
>>> requests.Session(base_url="http://www.example.com/a/b?a=b").get("").url
'http://www.example.com/a/b?a=b'
>>> requests.Session(base_url="http://www.example.com/a/b?a=b").get("?c=d").url
'http://www.example.com/a/b?c=d'

Please note that it might not work the same as the current version of httpx, which is mentioned in the original issue. For example, this is what httpx does:

>>> httpx.Client(base_url="http://www.example.com/a/b").get("c").url # different
URL('http://www.example.com/a/b/c')
>>> httpx.Client(base_url="http://www.example.com/a/b/").get("c").url # same
URL('http://www.example.com/a/b/c')

I didn't follow httpx's style. In my point of view, following RFC 3986 as what urljoin of urllib.parse does make more sense to me, since it works exactly the same as how the browser treats relative paths.

perklet commented 8 months ago

LGTM