-
-
Notifications
You must be signed in to change notification settings - Fork 858
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base_url strips relative path #846
Comments
The docs are missing a specific reference to that, but you need to make sure any subpaths you're using don't have a leading slash, else they will be interpreted as absolute paths. There's a PR to update the docs for that: #843 Personally I generally use |
I’d personally like HTTPX to be smart enough to not strip any prefix path too, and allow using a leading |
@florimondmanca Yeah I think that's a reasonable observation actually. It's not that our |
But what of the case where someone wants to clear the prefix? Should they create a new client, or perhaps pass an |
@StephenBrown2 Note that that sounds like a different use case/feature to me (that we don’t even support in the current state of things). |
Just tested a client with a base_url will append without a leading slash, and replace with one, and was surprised that it was not what either of us were talking about: Python 3.8.1 (default, Jan 10 2020, 09:36:37)
[Clang 11.0.0 (clang-1100.0.33.16)] on darwin
>>> import httpx
>>> http_client = httpx.Client(base_url="https://example.com/api/v1")
>>> append = http_client.get("endpoint")
>>> append.url
URL('https://example.com/api/endpoint')
>>> replace = http_client.get("/api/v2/new")
>>> replace.url
URL('https://example.com/api/v2/new')
>>> httpx.__version__
'0.11.1' And confirmed in iPython for AsyncClient: Python 3.8.1 (default, Jan 10 2020, 09:36:37)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.13.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: import httpx
In [2]: async with httpx.AsyncClient(base_url="https://example.com/api/v1") as http_client:
...: append = await http_client.get("endpoint")
...: print(f"{append.url=}")
...: replace = await http_client.get("/api/v2/new")
...: print(f"{replace.url=}")
...:
append.url=URL('https://example.com/api/endpoint')
replace.url=URL('https://example.com/api/v2/new') where I expected In [5]: http_client_ts = httpx.Client(base_url="https://example.com/api/v1/")
...: append = http_client_ts.get("endpoint")
...: append.url
...:
Out[5]: URL('https://example.com/api/v1/endpoint')
In [6]: replace = http_client_ts.get("/api/v2/new")
...: replace.url
...:
Out[6]: URL('https://example.com/api/v2/new') Here's the result table:
So either we document this (which I think is the proper solution), or "fix" it in some way. |
To be clear, the only usage I'd be expecting from >>> base_url = "https://example.com/api/v1" # Trailing slash is irrelevant
>>> client = httpx.Client(base_url=base_url)
>>> response = client.get("/endpoint") # Leading slash is *always* required
>>> response.url
URL("https://example.com/api/v1/endpoint") (Right now this is wrongly yielding URL("https://example.com/endpoint") as you noted too.) Formally: From an UX perspective, this is the most sensible thing to do in my POV as it is an extension of the default case of using |
Personally, now that I see the results, I think that "correct" trumps "surprising" in this case. There are three results currently:
The only case that is "surprising" to me at first glance is the last one, while the first two make intuitive sense to me and are what I would want. Though the last one makes sense after consideration, I currently work around it in the way I said above, with: client = httpx.Client(base_url="http://example.com/prefix/") # Use a trailing slash
client.verb(path.lstrip("/")) # Enforce no leading slash This also works with no base_url prefix: path = "/endpoint"
client = httpx.Client(base_url="http://example.com") # Use no trailing slash
request = client.get(path.lstrip("/")) # Enforce no leading slash
request.url == URL('http://example.com/endpoint') |
I'm just saying we shouldn't even have to think about "what are the possible cases?". Set a |
I'd expect to see just merge, no magic required:
|
So then to follow:
requiring a |
|
As long as those restrictions are documented with proper error messages (and the naive case prevented), I'm good with that. |
Milestoning to review for 1.0, since there's a potential behavioural change for us to introduce here. |
I found this change in behaviour quite surprising; I expected I don't expect that my opinion effect any change, but I don't feel the current behaviour is adequately documented. |
True, and if we have an open issue tracking adding further docs for this behavior: #1139 (comment) |
Hi,
I've noticed that if I do something like:
for further requests (e.g.
/messages
)/api/v1
will be stripped.Is it expected behavior?
Tested on httpx 0.11.1
Thanks!
The text was updated successfully, but these errors were encountered: