[go: up one dir, main page]

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

add Copy as Python Requests #6871

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open

add Copy as Python Requests #6871

wants to merge 12 commits into from

Conversation

Nriver
Copy link
@Nriver Nriver commented May 23, 2024

Description

ksnip_20240522-175559

Checklist

  • I have updated tests where applicable.
  • I have added an entry to the CHANGELOG.

Copy link
Member
@mhils mhils left a 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 Show resolved Hide resolved
is_json = False
if request.content:
try:
decoded_content = request.content.decode("utf-8")
Copy link
Member

Choose a reason for hiding this comment

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

Let's streamline this:

  1. If json.loads(request.content) works, go with that. json.loads accepts raw bytes, so we don't have to decode for that.
  2. If https://docs.mitmproxy.org/stable/api/mitmproxy/http.html#Message.text does not raise, assign body_str to repr(text)
  3. As a fallback, assign body_str to repr(request.content).

Technically steps 1-2 are optional, but definitely nice to have.

Copy link
Author

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.

Copy link
Member

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)}"

Copy link
Author

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]}

mitmproxy/addons/export.py Outdated Show resolved Hide resolved
code += "cookies = {\n"
for k, v in request.cookies.items():
code += f' "{k}": "{escape_quotes(v)}",\n'
code += "}\n"
Copy link
Member

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?

Copy link
Author

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"
Copy link
Member

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?

Copy link
Author

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)}", '
Copy link
Member

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

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