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

辛苦提供一个像charles那样可以直接获取到请求curl格式的功能 #640

Open
xiaohengdai opened this issue Apr 28, 2022 · 1 comment

Comments

@xiaohengdai
Copy link

Is your feature request related to a problem? Please describe.
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

Describe the solution you'd like
A clear and concise description of what you want to happen.

Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.

Additional context
Add any other context or screenshots about the feature request here.

@iwakura-lain
Copy link

iwakura-lain commented Mar 12, 2023

试着自己写了一个

def convert_curl(request, host):
    with requests.Session() as s:
        res = request_impl(s, method=request.get('method'), url=request.get('url'), headers=request.get('headers'), json=request.get('data'))
        strs = to_curl_impl(res, host)
        return strs

def request_impl(session, method, url,
            params=None, data=None, headers=None, cookies=None, files=None,
            auth=None, timeout=None, allow_redirects=True, proxies=None,
            hooks=None, stream=None, verify=None, cert=None, json=None):

        # Create the Request.
        req = Request(
            method=method.upper(),
            url=url,
            headers=headers,
            files=files,
            data=data or {},
            json=json,
            params=params or {},
            auth=auth,
            cookies=cookies,
            hooks=hooks,
        )
        prep = session.prepare_request(request=req)

        proxies = proxies or {}

        settings = session.merge_environment_settings(
            url=prep.url, proxies=proxies, stream=stream, verify=verify, cert=cert
        )

        return prep


def to_curl_impl(request, host, compressed=False, verify=False):
    parts = [
        ('curl', None),
        ('-X', request.method),
    ]

    for k, v in sorted(request.headers.items()):
        if k == 'Host':
            v = host
        parts += [('-H', '{0}: {1}'.format(k, v))]

    if request.body:
        body = request.body
        if isinstance(body, bytes):
            body = body.decode('utf-8')
        parts += [('-d', body)]

    if compressed:
        parts += [('--compressed', None)]

    if not verify:
        parts += [('--insecure', None)]

    parts += [(None, request.url)]

    flat_parts = []
    for k, v in parts:
        if k:
            flat_parts.append(quote(k))
        if v:
            flat_parts.append(quote(v))

    res = ' '.join(flat_parts)
    res = res + " --compressed"
    return res

在 get_flow_list_by_filter 里调用后加入到 info 里,前端使用即可

<span class="flow-list-item-copy-btn" @click.stop>
            <v-tooltip bottom>
              <template v-slot:activator="{ on, attrs }">
                <span v-bind="attrs" v-on="on">
                  <v-btn icon x-small plain>
                    <v-icon
                      x-small
                      color="blue"
                      v-clipboard:copy="item.request.curl"
                      v-clipboard:success="onCurlCopy"
                      v-clipboard:error="onCurlCopyError"
                    >mdi-content-copy</v-icon>
                  </v-btn>
                </span>
              </template>
              Copy cURL
            </v-tooltip>
</span>

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

No branches or pull requests

2 participants