You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 path.lstrip("/") before I do http_client.verb(path, **kwargs) in my calls.
I’d personally like HTTPX to be smart enough to not strip any prefix path too, and allow using a leading / on subsequent requests. Mostly because that’s the UX that test clients in web frameworks have been acustoming folks to. I’d consider the current behavior a bug, somewhat.
Reacted by Dmitry Figol, Elizabeth Bruce and Dubslow
@florimondmanca Yeah I think that's a reasonable observation actually. It's not that our .join(...) behaviour is broken, but that we shouldn't quite be using URL joinging semantics for clients with a base URL.
But what of the case where someone wants to clear the prefix? Should they create a new client, or perhaps pass an absolute_path=True arg to the client.verb call?
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:
where I expected append.url==URL('https://example.com/api/v1/endpoint'), replace.url was as expected. So, a bug? This is not the case if the base_url has a trailing slash.
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:
base=/api/v1/
base=/api/v1
path=/endpoint
/endpoint
/endpoint
path=endpoint
/api/v1/endpoint
/api/endpoint
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 is…
>>>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.urlURL("https://example.com/api/v1/endpoint")
(Right now this is wrongly yielding
URL("https://example.com/endpoint")
as you noted too.)
Formally: request.url = client.base_url.rstrip("/") + url — no need for any other smarts or advanced URL semantics.
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 base_url="https://example.com" and eg .get("/api/v1/endpoint") (which we currently correctly implement).
Personally, now that I see the results, I think that "correct" trumps "surprising" in this case. There are three results currently:
If you want to use a new base path, prepend the request path with a slash.
If you want to append path segments to the base_url, be sure the base_url ends with a slash and the request path does not begin with one.
If you want to replace the last segment of the path with a new one, don't put a slash at the end or beginning of the base_url or request path.
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 slashclient.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 slashrequest=client.get(path.lstrip("/")) # Enforce no leading slashrequest.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 base_url, it'll be prepended to all requested URL paths, with automatic handling of trailing slash madness. Wouldn't that be ideal? Also in line with what @dmfigol originally posted.
IMO cases when the requested URL doesn't start with either a scheme or / (as in your first example) should be forbidden. Or naively result in https://my-service/api/v1v2/messages.
Agreed, if we need multiple base_url it's a sign we are talking to multiple services and so we should use multiple separate clients.
I found this change in behaviour quite surprising; I expected urljoin() semantics and consistency with how a browser treats relative paths. ie: For a given base_url with a path, relative urls beginning with a / should resolve to a path at the server root and those without should be appended to the base_url.
I don't expect that my opinion effect any change, but I don't feel the current behaviour is adequately documented.
Activity
StephenBrown2 commentedon Mar 4, 2020
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
path.lstrip("/")before I dohttp_client.verb(path, **kwargs)in my calls.florimondmanca commentedon Mar 4, 2020
I’d personally like HTTPX to be smart enough to not strip any prefix path too, and allow using a leading
/on subsequent requests. Mostly because that’s the UX that test clients in web frameworks have been acustoming folks to. I’d consider the current behavior a bug, somewhat.lovelydinosaur commentedon Mar 5, 2020
@florimondmanca Yeah I think that's a reasonable observation actually. It's not that our
.join(...)behaviour is broken, but that we shouldn't quite be using URL joinging semantics for clients with a base URL.StephenBrown2 commentedon Mar 5, 2020
But what of the case where someone wants to clear the prefix? Should they create a new client, or perhaps pass an
absolute_path=Truearg to theclient.verbcall?florimondmanca commentedon Mar 5, 2020
@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).
StephenBrown2 commentedon Mar 5, 2020
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:
And confirmed in iPython for AsyncClient:
where I expected
append.url==URL('https://example.com/api/v1/endpoint'),replace.urlwas as expected. So, a bug? This is not the case if thebase_urlhas a trailing slash.Here's the result table:
/api/v1//api/v1/endpoint/endpoint/endpointendpoint/api/v1/endpoint/api/endpointSo either we document this (which I think is the proper solution), or "fix" it in some way.
florimondmanca commentedon Mar 5, 2020
To be clear, the only usage I'd be expecting from
base_urlis…(Right now this is wrongly yielding
as you noted too.)
Formally:
request.url = client.base_url.rstrip("/") + url— no need for any other smarts or advanced URL semantics.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
base_url="https://example.com"and eg.get("/api/v1/endpoint")(which we currently correctly implement).StephenBrown2 commentedon Mar 5, 2020
Personally, now that I see the results, I think that "correct" trumps "surprising" in this case. There are three results currently:
base_url, be sure the base_url ends with a slash and the request path does not begin with one.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:
This also works with no base_url prefix:
florimondmanca commentedon Mar 5, 2020
I'm just saying we shouldn't even have to think about "what are the possible cases?". Set a
base_url, it'll be prepended to all requested URL paths, with automatic handling of trailing slash madness. Wouldn't that be ideal? Also in line with what @dmfigol originally posted.dmfigol commentedon Mar 5, 2020
I'd expect to see just merge, no magic required:
base_url="https://my-service/api/v1"and.get("/messages")will result inhttps://my-service/api/v1/messagesbase_url="https://my-service/api/v1/"and.get("messages")will result inhttps://my-service/api/v1/messagesbase_url="https://my-service/api/v1/"and.get("https://my-other-service/api/v1/me")will result inhttps://my-other-service/api/v1/meStephenBrown2 commentedon Mar 5, 2020
So then to follow:
base_url="https://my-service/api/v1"and.get("v2/messages")will result inhttps://my-service/api/v1/v2/messagesbase_url="https://my-service/api/v1"and.get("/api/v2/messages")will result inhttps://my-service/api/v1/api/v2/messagesrequiring a
v1_client = httpx.Client(base_url=".../v1")andv2_client = httpx.Client(base_url=".../v2")is probably an acceptable compromise.florimondmanca commentedon Mar 5, 2020
@StephenBrown2
/(as in your first example) should be forbidden. Or naively result inhttps://my-service/api/v1v2/messages.base_urlit's a sign we are talking to multiple services and so we should use multiple separate clients.StephenBrown2 commentedon Mar 5, 2020
As long as those restrictions are documented with proper error messages (and the naive case prevented), I'm good with that.
lovelydinosaur commentedon Jul 30, 2020
Milestoning to review for 1.0, since there's a potential behavioural change for us to introduce here.
james-emerton commentedon Sep 2, 2020
I found this change in behaviour quite surprising; I expected
urljoin()semantics and consistency with how a browser treats relative paths. ie: For a given base_url with a path, relative urls beginning with a/should resolve to a path at the server root and those without should be appended to thebase_url.I don't expect that my opinion effect any change, but I don't feel the current behaviour is adequately documented.
florimondmanca commentedon Sep 3, 2020
True, and if we have an open issue tracking adding further docs for this behavior: #1139 (comment)