Skip to content

Fix JSON parsing for python requests #6

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 2 commits into from
Apr 5, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/targets/python/requests.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ module.exports = function (source, options) {
switch (source.postData.mimeType) {
case 'application/json':
if (source.postData.jsonObj) {
code.push('payload = %s', helpers.literalRepresentation(source.postData.jsonObj, opts))
code.push('payload = %s', JSON.stringify(source.postData.jsonObj))
Copy link
Member

Choose a reason for hiding this comment

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

This should work, and it's a good fix.

That said, I think it might be worth fixing this inside literalRepresentation instead, because this bug will apply to anywhere where that's used on an object like your example.

The idea is that this method is supposed to return a Python literal object value for the JS value, very similar to JSON.stringify.

What happens if we just use JSON.stringify for the string case there, on this line?

Copy link
Member

Choose a reason for hiding this comment

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

Ah, in fact there are a few cases where this current fix won't work, some of which are in the failing tests here. Some notable examples:

  • { x: true } becomes { "x": true } but booleans in Python must be capitalized (True)
  • { x: null } becomes { "x": null } but null doesn't exist in Python - it has to be None.

literalRepresentation already handles this, so I think just fixing the string case there will solve this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

adding JSON.stringify on this line you mentioned fixes the issue. updated in the commit.

jsonPayload = true
hasPayload = true
}
Expand Down