Fix URL params to merge with existing query parameters instead of replacing them #3618
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes a long-standing issue where
URL(url, params=params)
andRequest(method, url, params=params)
would completely replace existing query parameters in the URL instead of merging them. This behavior was inconsistent with the Pythonrequests
library and unintuitive for users.Problem
Previously, when creating a URL or Request with additional parameters:
The original query parameters (
token=abc123
) were completely discarded.Solution
After this fix, parameters are intelligently merged:
Behavior Details
The new logic handles various cases intelligently:
Non-empty params: Merged with existing query parameters
Empty dict params: Preserves existing query parameters
None params: Preserves existing query parameters
Overlapping parameter names: New values override old ones
QueryParams objects: Used directly (preserves existing copy_* method behavior)
Compatibility
copy_*
methods work unchangedFiles Changed
httpx/_urls.py
: Updated URL.init params handling logictests/models/test_url.py
: Updated test expectations for merge behaviortests/models/test_requests.py
: Updated test expectations for merge behaviorTesting
Added comprehensive tests covering:
All existing tests pass with the new behavior.
Related Issues
Fixes #652 -
params
overrides query string in urlBreaking Changes
This is technically a breaking change in behavior, but it fixes unintuitive behavior that was likely causing bugs in user code. The new behavior is:
requests
libraryUsers who were relying on the old replacement behavior can achieve the same result by manually constructing URLs without existing query parameters.