Skip to content

Instantly share code, notes, and snippets.

@jelmervdl
Last active February 21, 2017 10:05
Show Gist options
  • Save jelmervdl/65a001bc7e3f423160ed429093f57d34 to your computer and use it in GitHub Desktop.
Save jelmervdl/65a001bc7e3f423160ed429093f57d34 to your computer and use it in GitHub Desktop.
Turns a requests' request object into something you can paste in your terminal.
def request_as_curl_command(request):
'''
Turns a requests' request object into something you can pase in your terminal.
Usage:
response = requests.request(...)
print(request_as_curl_command(response.request))
'''
return "curl -X {method} -H {headers} -d '{data}' '{uri}'".format(
method=request.method,
headers=" -H ".join(["'{}'".format('{0}: {1}'.format(k, v).replace('\\', '\\\\').replace("'", "\\'")) for k, v in request.headers.items()]),
data=request.body.replace('\\', '\\\\').replace("'", "\\'"),
uri=request.url)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment