Every API documentation includes curl examples. You copy the curl command, it works in your terminal, and then you need to make the same request from your application code. Converting that curl command to Python, JavaScript, Go, or any other language by hand is tedious and error-prone.
I have manually translated hundreds of curl commands to application code, and the patterns are always the same. It is mechanical translation that should be automated.
What a curl command actually specifies
A curl command encodes a complete HTTP request:
curl -X POST 'https://api.example.com/users' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer tok_abc123' \
-d '{"name": "Alice", "email": "alice@example.com"}'
This specifies: method (POST), URL, headers (Content-Type, Authorization), and body (JSON payload). Every language's HTTP client needs the same information, just in different syntax.
The translation patterns
Python (requests):
import requests
response = requests.post(
'https://api.example.com/users',
headers={
'Content-Type': 'application/json',
'Authorization': 'Bearer tok_abc123',
},
json={'name': 'Alice', 'email': 'alice@example.com'},
)
JavaScript (fetch):
const response = await fetch('https://api.example.com/users', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer tok_abc123',
},
body: JSON.stringify({
name: 'Alice',
email: 'alice@example.com',
}),
});
Go (net/http):
body := strings.NewReader(`{"name":"Alice","email":"alice@example.com"}`)
req, _ := http.NewRequest("POST", "https://api.example.com/users", body)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer tok_abc123")
client := &http.Client{}
resp, _ := client.Do(req)
The information is identical. The syntax differs. This is exactly the kind of translation a tool should handle.
curl flags that complicate things
Simple curl commands are straightforward. Complex ones have flags that affect the translation:
-k / --insecure: Disables SSL certificate verification. In Python: verify=False. In Node: requires setting rejectUnauthorized: false on the agent.
-L / --location: Follows redirects. Most HTTP libraries follow redirects by default, but some do not.
-u user:pass: Basic authentication. The translation depends on whether the library has built-in basic auth support.
--data-urlencode: URL-encodes the data. The translation needs to use form encoding rather than raw body.
-F / --form: Multipart form upload. Significantly different syntax in every language.
--compressed: Accept compressed responses. Most libraries handle this automatically.
-b / --cookie: Send cookies. Requires cookie jar management in some languages.
The edge cases
Multiline curl commands: Commands spread across multiple lines with backslash continuation. Need to be properly joined before parsing.
Quoted vs unquoted values: 'single quotes' and "double quotes" behave differently in shell. The converter needs to handle both.
Environment variables in curl: Commands like curl -H "Authorization: Bearer $TOKEN" reference shell variables. The converter should note these rather than including the literal variable reference.
Multiple -H flags: Each header is a separate flag. Need to collect them all into a headers dictionary/map.
The converter
For instant translation of curl commands to any language, I built a curl converter that parses the full curl syntax and generates clean, idiomatic code in Python, JavaScript, Go, PHP, Ruby, and more.
I'm Michael Lip. I build free developer tools at zovo.one. 500+ tools, all private, all free.
Top comments (0)