-
-
Save LLyaudet/00b210cf50ebf52a4e47aea126c7524c to your computer and use it in GitHub Desktop.
Django HTTP Request to curl command (for replay)
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
""" | |
Convert a Django HTTPRequest object (or dictionary of such a request) into a | |
cURL command. | |
""" | |
def get_curl(request): | |
def convert(word, delim='-'): | |
return delim.join(x.capitalize() or '_' for x in word.split('_')) | |
def get_headers(request): | |
headers = { | |
convert(name[5:]): value | |
for name, value in request.META.items() | |
if name.startswith('HTTP_') | |
} | |
return headers | |
headers = get_headers(request) | |
url = '{server}{path}?{query}'.format( | |
server=request.META['SERVER_NAME'], | |
path=request.META['PATH_INFO'], | |
query=request.META['QUERY_STRING'], | |
) | |
return 'curl {headers} "{url}"'.format( | |
headers=' '.join(('-H "%s: %s"' % (h, v)) for h, v in headers.items()), | |
url=url) | |
""" | |
Laurent Lyaudet : I simplified the original code of asfaltboy to make it work in my use case. | |
(I had request.META directly available in DRF.) | |
Copy the function above where needed and add something like that in your view. | |
logger.info(f"AAAAAAAAAAAAAA {get_curl(request)}") | |
If it does not fit your need, | |
you may open an issue here and I'll try to help. | |
But you may find a faster answer back in asfaltboy original code. | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment