Skip to content

Instantly share code, notes, and snippets.

@DBoyara
Created September 3, 2024 05:04
Show Gist options
  • Select an option

  • Save DBoyara/688b835607f1518c34a7485f45bdee44 to your computer and use it in GitHub Desktop.

Select an option

Save DBoyara/688b835607f1518c34a7485f45bdee44 to your computer and use it in GitHub Desktop.
generate curl command from aiohttp
async def generate_curl_command(method, url, **kwargs):
curl_cmd = f"curl -X {method.upper()} '{url}'"
headers = kwargs.get('headers')
if headers:
for k, v in headers.items():
curl_cmd += f" -H '{k}: {v}'"
params = kwargs.get('params')
if params:
query_string = "&".join([f"{k}={v}" for k, v in params.items()])
curl_cmd += f"?{query_string}"
data = kwargs.get('data')
json_data = kwargs.get('json')
form_data = kwargs.get('data') if isinstance(kwargs.get('data'), aiohttp.FormData) else None
if json_data:
curl_cmd += f" -d '{json.dumps(json_data)}'"
elif form_data:
for field in form_data._fields:
name = field[0]
value = field[2]
if isinstance(value, tuple):
filename, file_obj, content_type = value
curl_cmd += f" -F '{name}=@{file_obj.name};type={content_type}'"
else:
curl_cmd += f" -F '{name}={value}'"
elif data:
curl_cmd += f" -d '{data}'"
return curl_cmd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment