-
Notifications
You must be signed in to change notification settings - Fork 8
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
Fix JSON parsing for python requests #6
Conversation
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.
Good catch! This error makes sense, that definitely needs fixing. Would be nice to do so slightly more generally to cover other cases too.
It would also be good to include a test for this, so we don't break it again in future.
src/targets/python/requests.js
Outdated
@@ -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)) |
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.
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?
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.
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 beNone
.
literalRepresentation already handles this, so I think just fixing the string case there will solve this.
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.
adding JSON.stringify on this line you mentioned fixes the issue. updated in the commit.
This looks good to me, merged! 👍 Unfortunately all the tests are broken because Kong/Insomnia have seemingly shutdown the mockbin.com/har endpoint, so every 'request validation' test now fails. What a mess. I've just fixed this on main though (just skipping validation tests, to only check output instead) so hopefully this'll pass correctly on top of that 😄 |
Problem
JSON payload for python requests client is not parsed correctly. Specifically, this payload:
will result in this output
Problem is caused by the
{\\n
and\"
.Solution
Used JSON.stringify to convert the jsonObj to string.
Results in correct output:
Indentation can also be added by passing appropriate arguments to JSON.stringify.