Skip to content

Instantly share code, notes, and snippets.

@davydany
Last active January 25, 2023 17:15
Show Gist options
  • Save davydany/b5dd9e2f2a2a34715dcde79b92cb2b0d to your computer and use it in GitHub Desktop.
Save davydany/b5dd9e2f2a2a34715dcde79b92cb2b0d to your computer and use it in GitHub Desktop.
A simple function that can take any cURL statement and creates a Django HTTP Request object from it.
import django.http
def convert_curl_to_httprequest(curl_statement):
import re
# Split by spaces
curl_parts = curl_statement.split(' ')
# Get method
method = curl_parts[1]
# Get URL
url = curl_parts[2]
# Get Headers
headers = dict()
for part in curl_parts:
if part.startswith('-H'):
# Split at ':'
header_parts = part.split(':')
if header_parts:
# Strip quotes
header_parts[0] = header_parts[0].strip('"')
# Add to dictionary
headers[header_parts[0][2:]] = header_parts[1]
# Get data
data_string = None
for part in curl_parts:
if part.startswith('-d'):
data_string = re.split(r'[="]\s*', part)[2]
break
# Create Request
request = django.http.HttpRequest()
request.method = method
request.path = url
request.headers = headers
request.body = data_string
return request
# Usage
curl_statement = 'curl -X POST -H "Content-Type: application/json" -d "{"name": "John", "age": 30}" http://www.example.com/api/users
request = convert_curl_to_httprequest(curl_statement)
print(request.method, request.path, request.headers, request.body)
# Output
# POST http://www.example.com/api/users {'Content-Type': 'application/json'} {'name': 'John', 'age': 30}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment