Last active
February 21, 2017 10:05
-
-
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.
This file contains hidden or 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
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