-
-
Notifications
You must be signed in to change notification settings - Fork 4k
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
add Copy as Python Requests #6871
base: main
Are you sure you want to change the base?
Conversation
refine json data parse result
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.
Thanks for the PR!
mitmproxy/addons/export.py
Outdated
is_json = False | ||
if request.content: | ||
try: | ||
decoded_content = request.content.decode("utf-8") |
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.
Let's streamline this:
- If
json.loads(request.content)
works, go with that. json.loads accepts raw bytes, so we don't have to decode for that. - If https://docs.mitmproxy.org/stable/api/mitmproxy/http.html#Message.text does not raise, assign body_str to repr(text)
- As a fallback, assign body_str to repr(request.content).
Technically steps 1-2 are optional, but definitely nice to have.
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.
Not sure how to do step 2.
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.
I was thinking of something like this:
from pprint import pformat
try:
body_val = json.loads(request.content)
except ValueError:
try:
body_val = request.text
except ValueError:
body_val = request.content
body_str = f"body = {pformat(body_val, indent=4, sort_dicts=False)}"
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.
And this, will create weird code format like this
body = { 'string': 'Hello, world!',
'number': 42,
'float': 3.14,
'boolean': True,
'nullValue': None,
'object': {'name': 'John', 'age': 30},
'array': [1, 2, 3, 4]}
code += "cookies = {\n" | ||
for k, v in request.cookies.items(): | ||
code += f' "{k}": "{escape_quotes(v)}",\n' | ||
code += "}\n" |
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.
Can we use pformat here instead of manually constructing stuff?
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.
No. This will create something like this
cookies = MultiDictView[['cookie', 'chocolate_chip'], ['session_id', 'abc123'], ['user_id', '987654321']]
code += "headers = {\n" | ||
for k, v in request.headers.items(): | ||
code += f' "{k}": "{escape_quotes(v)}",\n' | ||
code += "}\n" |
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.
Can we use pformat here instead of manually constructing stuff?
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.
Similar to the above, this will create
headers = Headers[(b'header', b'qvalue')]
|
||
param = "json=body" if is_json else "data=body" | ||
code += ( | ||
f'res = requests.request(method="{escape_quotes(request.method)}", ' |
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.
Let's use repr()
here, then we don't need any custom escaping functions
Description
Checklist