Created
July 22, 2023 08:24
-
-
Save xiongnemo/de902278f9b587ce24c23855b0eaf1bb to your computer and use it in GitHub Desktop.
Transform "Copy as cURL (bash)" text to request session.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
headers = {} | |
post_flag = False | |
data_flag = False | |
data = None | |
print('Provide your raw "Copy as cURL (bash)" with preceding whitespaces below:\n') | |
while True: | |
a = input() | |
if a.startswith('curl'): | |
url = a.lstrip('curl').rstrip('\\').strip().strip("'") | |
continue | |
if a.startswith(' -H'): | |
line = a.rstrip("' \\").lstrip(" -H '") | |
key = line.split(':')[0].strip() | |
value = line.replace(f'{key}:', '').strip() | |
headers[key] = value | |
if a.startswith(' --data-raw'): | |
line = a.rstrip("' \\").lstrip(" --data-raw '") | |
data = line | |
data_flag = True | |
post_flag = True | |
if a.startswith(' --compressed'): | |
break | |
print(f""" | |
=== OUTPUT === | |
headers = {headers} | |
import requests | |
s = requests.session() | |
s.headers.update(headers) | |
{'post_data = "' + data + '"' if data_flag else ''} | |
r = s.{'post' if post_flag else 'get'}('{url}'{', data=post_data' if data_flag else ''}) | |
# json? | |
# print(r.json()) | |
# text? | |
print(r.text)""") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment