Skip to content

Instantly share code, notes, and snippets.

@JanChec
Created August 13, 2019 11:10
Show Gist options
  • Save JanChec/d6339c20988e72e2aaba979b3606e9ce to your computer and use it in GitHub Desktop.
Save JanChec/d6339c20988e72e2aaba979b3606e9ce to your computer and use it in GitHub Desktop.
Convert raw HTTP request to CURL format (useful for https requests, use nc otherwise)
#!/usr/bin/env python3
import email
import io
import sys
def run():
request_text = sys.stdin.read()
request_line, headers_raw = request_text.strip().split('\n', 1)
message = email.message_from_file(io.StringIO(headers_raw))
headers = dict(message.items())
headers_inline = [f"-H '{header_key}: {header_value}'"
for header_key, header_value in headers.items()]
method, url, _ = request_line.split(' ')
command_parts = [f"curl -X{method}"] + headers_inline + [f"-d '{message.get_payload()}'", f"'{url}'"]
print(" ".join(command_parts))
if __name__ == '__main__':
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment