Skip to content

Fix base_url query parameters corrupted during URL merging - #3766

Open
worksbyfriday wants to merge 1 commit into
encode:masterfrom
worksbyfriday:fix-base-url-query-corruption
Open

worksbyfriday wants to merge 1 commit into
encode:masterfrom
worksbyfriday:fix-base-url-query-corruption

Conversation

@worksbyfriday

Copy link
Copy Markdown

Summary

  • Fix _enforce_trailing_slash() appending / after query string in raw_path, corrupting ?data=1 into ?data=1/
  • Fix _merge_url() concatenating paths without separating query string from path component
  • Both methods now split raw_path at ? before path manipulation, then rejoin with query intact

Fixes #3614

Test plan

  • Added test_merge_url_with_base_url_query_params covering empty merge path and subpath merge
  • All existing merge URL tests pass (7/7)

When base_url contains query parameters (e.g. "https://example.com/get?data=1"),
_enforce_trailing_slash() appended "/" to raw_path which includes the query string,
corrupting "?data=1" into "?data=1/". Similarly, _merge_url() concatenated paths
without separating the query string from the path component.

Fix both methods to split raw_path at "?" before manipulating the path, then
rejoin with the query string intact.

Fixes encode#3614

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

@agammann agammann left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At httpx/_client.py:420-422 on head 18927fa, the new concatenation appends a second ? when the relative request URL has its own query. I reproduced with Client(base_url="https://example.com/get?data=1").build_request("GET", "/users?other=2"): the result is https://example.com/get/users?other=2?data=1, and request.url.params["other"] is "2?data=1" rather than "2". The seven URL merge tests pass, but the new case only uses relative URLs without a query. Could we handle the relative query separately and add a test with queries on both the base and request URLs? Whatever precedence is chosen for the base query, the request parameter value should remain intact.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Corruption of query parameter value when using base_url with query string

2 participants