Skip to content

Instantly share code, notes, and snippets.

@xiongnemo
Created July 22, 2023 08:24
Show Gist options
  • Save xiongnemo/de902278f9b587ce24c23855b0eaf1bb to your computer and use it in GitHub Desktop.
Save xiongnemo/de902278f9b587ce24c23855b0eaf1bb to your computer and use it in GitHub Desktop.
Transform "Copy as cURL (bash)" text to request session.
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