Skip to content

Instantly share code, notes, and snippets.

@matthewlmcclure
Created July 12, 2013 01:25
Show Gist options
  • Save matthewlmcclure/5980698 to your computer and use it in GitHub Desktop.
Save matthewlmcclure/5980698 to your computer and use it in GitHub Desktop.
Return a curl command string view of a Requests PreparedRequest
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