Repository navigation
Conversation
|
All CI passing on Python 3.9-3.13, ready for merge |
|
Friendly ping - any chance someone could take a look at this when they get a chance? Happy to make any changes if needed. |
…ement When a base_url contained query parameters, _enforce_trailing_slash() was appending a slash to the entire raw_path (including the query string), which corrupted the query parameter values. For example: - Before: base_url="https://api.com?data=1" became "https://api.com?data=1/" (query param corrupted to "data=1/" instead of "data=1") - After: base_url="https://api.com?data=1" becomes "https://api.com/?data=1" (query param correctly preserved as "data=1") The fix splits the raw_path into path and query components, adds the trailing slash only to the path portion, then recombines them. Fixes encode#3614
2fba056 to
b2adc52
Compare
|
Heya... thanks for this. Development of the next version is shifting across to a rewrite. |
OKAN-SOFTWARE
left a comment
There was a problem hiding this comment.
Reviewed commit b2adc52.
The change to _enforce_trailing_slash() correctly prevents the trailing slash from being appended directly to an existing query value.
However, the issue appears to remain when a relative URL is passed to Client.build_request().
For example:
client = httpx.Client(base_url="https://example.com/api?data=1")
request = client.build_request("GET", "users")After the proposed change, the stored base URL has this raw path:
/api/?data=1
But _merge_url() still performs:
merge_raw_path = self.base_url.raw_path + merge_url.raw_path.lstrip(b"/")Consequently, the resulting raw path becomes:
/api/?data=1users
The relative path is appended to the query value instead of the URL path.
The new tests verify client.base_url, but they don't exercise an actual request built from that base URL.
Could we extend the fix to _merge_url() so the path and query are handled separately?
A regression test using Client.build_request("GET", "users") with a query-bearing base_url would verify that the relative path remains in the path component and the existing query value isn't corrupted.
I verified the raw-path concatenation behavior independently. GitHub reports a passing test workflow; I have not run the complete HTTPX test suite locally.
Summary
Fixes #3614
When a
base_urlcontains query parameters, the trailing slash enforcement was corrupting the query parameter values by appending the slash to the query string instead of just the path.Problem
The
_enforce_trailing_slash()method was appending/to the entireraw_path, which includes both the path and query string. For a URL like/get?data=1, this became/get?data=1/, corrupting the query parameter value.Solution
Modified
_enforce_trailing_slash()in_client.pyto:raw_pathinto path and query components (using?as delimiter)Examples
base_url with query parameters:
base_url with trailing slash and query:
base_url without query (unchanged behavior):
Testing
tests/client/test_client.py