-
Notifications
You must be signed in to change notification settings - Fork 12
Non blocking json enc/dec #178
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
Verify Content-Type header handling and encoding behavior.
The manual JSON serialization approach replaces httpx's built-in
jsonparameter handling. While this offloads CPU-bound work to a thread pool, consider these points:Encoding:
json.dumps()returns astr, which httpx will encode using the charset from Content-Type. Since no charset is specified, it defaults to UTF-8, which is correct for JSON.Header conflict: If
call_kwargs["headers"]already contains aContent-Typeheader, line 365 will overwrite it. While this is likely intentional, ensure this doesn't break any callers that explicitly set Content-Type.Error handling: If
json_bodycontains non-serializable objects or circular references,json.dumps()will raise an exception in the thread pool, which will propagate as expected.Behavior parity: Verify that this manual approach preserves all behaviors of httpx's native
jsonparameter (e.g., settingContent-Length, handling empty bodies).Consider adding a test to verify that the manual JSON encoding produces identical requests to httpx's native
jsonparameter:🏁 Script executed:
Length of output: 0
🏁 Script executed:
Length of output: 1186
🏁 Script executed:
Length of output: 2502
🏁 Script executed:
Length of output: 811
🏁 Script executed:
Length of output: 329
Length of output: 1690
🏁 Script executed:
Length of output: 440
🏁 Script executed:
Length of output: 0
🏁 Script executed:
Length of output: 2701
🌐 Web query:
💡 Result:
Short answer: httpx's json= parameter JSON-encodes the value and sets Content-Type: application/json for you (unless you override the header). [1][2]
Example: httpx.post(url, json={"a":1}) -> body is JSON and http header Content-Type: application/json, but if you pass headers={"Content-Type": "..."} that explicit value is used instead. [1][2]
Sources:
[1] httpx API (json parameter docs).
[2] httpx Quickstart (Sending JSON encoded data).
Fix header overwrite to respect explicit Content-Type if provided.
The manual JSON serialization unconditionally overwrites any caller-provided Content-Type header (line 365), which differs from httpx's native
jsonparameter. When you passjson=to httpx, it sets Content-Type only if not explicitly provided by the caller; this code always overwrites.Behavioral differences confirmed:
headers={"Content-Type": ...}passed by callerUpdate line 365 to only set Content-Type if not already present:
This preserves the ability for callers to override Content-Type while maintaining the default JSON behavior.
🤖 Prompt for AI Agents