Created
July 12, 2013 01:25
-
-
Save matthewlmcclure/5980698 to your computer and use it in GitHub Desktop.
Return a curl command string view of a Requests PreparedRequest
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
def headers_as_curl(headers): | |
header_args = [ | |
" -H '{key}: {value}'".format(key=key, value=value) | |
for key, value in headers.iteritems() | |
] | |
return ''.join(header_args) | |
def request_as_curl(preparedrequest): | |
body = preparedrequest.body if preparedrequest.body else '' | |
return ( | |
"curl -X {method} '{url}' {headers} --data-binary '{body}'" | |
).format( | |
method=preparedrequest.method, | |
url=preparedrequest.url, | |
headers=headers_as_curl(preparedrequest.headers), | |
body=body | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment