How to use it
- Paste the curl command. A leading
curlor$ curlis fine, and so are multi-line commands ending in\. - Copy the Python code and run it with the requests library installed (
pip install requests). - Check the note under the result for anything that was not converted.
How it works
The command is split like a shell would split it, then each option is read:
- The method comes from -X, or is POST when there is a body or form, HEAD with -I, and GET otherwise. Standard methods use
requests.get,requests.postand so on; others userequests.request. - Headers (-H, -A, -b, -e, and -u as Basic auth) go into a headers dict.
- A JSON body with a JSON content type becomes
json_data(true → True, null → None); other bodies become adatastring; -F fields becomefiles. With -G, data is added to the URL instead.
Worked example
curl -X POST https://api.example.com/items -H 'Content-Type: application/json' -d '{"name":"x","ok":true}'
becomes
import requests
headers = {
'Content-Type': 'application/json',
}
json_data = {
'name': 'x',
'ok': True,
}
response = requests.post('https://api.example.com/items', headers=headers, json=json_data)
print(response.status_code)
print(response.text)
Tips
- Copied browser commands contain cookies and tokens. Remove them or move them to environment variables before sharing code.
- Use response.json() instead of response.text when the API returns JSON.
- For JavaScript, convert the same command with cURL to fetch.