Skip to content
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

Merged
merged 2 commits into from
Apr 5, 2024

Conversation

Fr0stM0urne
Copy link
Contributor

Problem
JSON payload for python requests client is not parsed correctly. Specifically, this payload:

{
    "method": "POST",
    "url": "http://mockbin.com/har",
    "httpVersion": "HTTP/1.1",
    "cookies": [],
    "headers": [
        {
            "name": "content-type",
            "value": "application/json"
        }
    ],
    "queryString": [],
    "postData": {
        "mimeType": "application/json",
        "text": "{\"field\":\"value{\\n n1 \"}"
    }
}

will result in this output

import requests

url = "http://mockbin.com/har"

payload = { "field": "value{
 n1 " }
headers = { "content-type": "application/json" }

response = requests.post(url, json=payload, headers=headers)

print(response.text)

Problem is caused by the {\\n and \".

Solution
Used JSON.stringify to convert the jsonObj to string.

Results in correct output:

import requests

url = "http://mockbin.com/har"

payload = {"field":"value{\n n1 "}
headers = { "content-type": "application/json" }

response = requests.post(url, json=payload, headers=headers)

print(response.text)

Indentation can also be added by passing appropriate arguments to JSON.stringify.

Copy link
Member

@pimterry pimterry left a 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.

@@ -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.

@pimterry pimterry merged commit 57515e2 into httptoolkit:main Apr 5, 2024
2 of 7 checks passed
@pimterry
Copy link
Member

pimterry commented Apr 5, 2024

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 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants