Homeโ€บArticlesโ€บHow Toโ€บHow to Convert cURL to Code
HOW TO

How to Convert a cURL Command to Python, JS, or Node

Step-by-step guide to converting any cURL command into Python requests, JavaScript fetch, or Node.js axios code, with worked examples for each.

Reviewed by the thecalcu.com team ยท Last updated August 4, 2026

API documentation almost always shows example requests as cURL commands, but using that API in your own application means translating the command into your language's HTTP client syntax. This guide walks through that conversion by hand, then shows how a converter tool handles the same process automatically.

What You Need

  • A cURL command, copied from API documentation or your browser's Network tab
  • A target language in mind (Python, JavaScript, or Node.js)
  • The relevant HTTP library installed if you're using Python's requests or Node's axios

The cURL to Code Converter automates every step below. Paste your command, pick a language, and get working code instantly.


Step 1: Identify the URL and Method

Find the URL in your cURL command. It's usually the only argument without a leading dash. Check for an explicit -X or --request flag specifying the HTTP method. If there isn't one, cURL defaults to GET unless a -d or --data flag is present, in which case it defaults to POST instead.

Example cURL command:

curl -X POST https://api.example.com/users \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -d '{"name": "Asha", "role": "admin"}'

Here, the method is explicitly POST and the URL is https://api.example.com/users.


Step 2: Extract All Headers

List every -H or --header flag and split each on the first colon to get the header name and value separately. In the example above, there are two headers: Content-Type: application/json and Authorization: Bearer YOUR_TOKEN.


Step 3: Extract the Request Body

Find any -d, --data, or --data-raw flag. That's your request body. In the example, the body is {"name": "Asha", "role": "admin"}, a JSON string.


Step 4: Write the Python Version

import requests

url = 'https://api.example.com/users'
headers = {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer YOUR_TOKEN',
}
data = '{"name": "Asha", "role": "admin"}'

response = requests.request('POST', url, headers=headers, data=data)
print(response.status_code)
print(response.text)

Step 5: Write the JavaScript Version

fetch('https://api.example.com/users', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer YOUR_TOKEN',
  },
  body: '{"name": "Asha", "role": "admin"}',
})
  .then((res) => res.text())
  .then((text) => console.log(text))
  .catch((err) => console.error(err));

Step 6: Write the Node.js (axios) Version

const axios = require('axios');

axios({
  method: 'post',
  url: 'https://api.example.com/users',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer YOUR_TOKEN',
  },
  data: '{"name": "Asha", "role": "admin"}',
})
  .then((res) => console.log(res.data))
  .catch((err) => console.error(err));

Step 7: Replace Credentials Before Sharing

If your original command included a real API key or token (as in YOUR_TOKEN above), swap it for an environment variable reference before committing the converted code to version control or sharing it with teammates.


Common Mistakes to Avoid

Forgetting the default method change. A cURL command with -d but no -X defaults to POST, not GET. Make sure your converted code matches that.

Mishandling multi-line commands. Trailing backslashes mean the command continues on the next line. Don't treat each line as separate.

Leaving real credentials in shared code. Scrub API keys and tokens before committing or sharing converted code, every time, not just when you remember.

Missing quoted string escaping. JSON bodies inside single-quoted shell strings can contain escaped characters, and those need careful handling when you reconstruct the string in your target language.

Key Terms

  • cURL: a command-line tool for making HTTP requests, commonly used in API documentation to show example requests.
  • JSON: the data format most commonly used for API request and response bodies.
  • REST API: a web service architecture style using standard HTTP methods (GET, POST, PUT, DELETE) for resource operations.

Frequently Asked Questions

Where do I get a cURL command to convert?
Most API documentation includes example cURL commands directly. You can also copy one from your browser's developer tools: open the Network tab, right-click any request, and select 'Copy as cURL' to get the exact request your browser made.
Why does cURL default to GET unless I add -d?
cURL defaults to a GET request unless a request body is provided via -d or --data, in which case it switches to POST automatically. A code converter needs to replicate that same default rather than assuming GET every time.
How do I handle multi-line cURL commands with backslashes?
A trailing backslash (\) at the end of a line tells the shell the command continues on the next line. A correct parser treats these as continuations and reconstructs the full command before processing it, so paste it exactly as copied, backslashes included.
What's the difference between the Python, JavaScript, and Node.js output?
Python output uses the `requests` library, JavaScript output uses the browser-native `fetch` API, and Node.js output uses `axios`. Pick whichever matches the environment you're actually working in.
Do I need to install anything to run the converted code?
Python's `requests` and Node.js's `axios` are external packages, so you'll need `pip install requests` or `npm install axios` first. JavaScript's `fetch` works natively in modern browsers and recent Node.js versions with no install needed.
How are headers handled in the conversion?
Each -H or --header flag in the cURL command becomes a key-value entry in the target language's headers object or dictionary, keeping the exact header name and value from the original command.
What happens to authentication flags like -u?
Basic auth credentials passed via -u user:pass get translated into the target language's equivalent: a tuple passed to the auth parameter in Python's requests, or an auth object with username and password fields in axios.
Should I remove API keys before sharing converted code?
You should. If your original cURL command included a real API key, bearer token, or password, the converted code carries that same credential over. Replace real credentials with environment variables or placeholders before committing the code or sharing it with anyone.
Does this work for cURL commands with file uploads?
Basic conversion handles headers, body data, and authentication well, but flags like file uploads (-F) often need manual translation since multipart form handling differs between languages and libraries.
Can I convert the same cURL command to multiple languages?
Yes. The underlying request (URL, method, headers, body) stays the same regardless of target language, so you can convert one cURL command to Python, JavaScript, and Node.js separately and get equivalent code in each, which is handy when the same API call needs to live in different parts of a polyglot codebase.

Related Articles

GUIDE

Developer Toolbox Guide โ€” Essential Online Tools

HOW TO

How to Format JSON Data

COMPARISON

REST vs GraphQL โ€” API Architecture Comparison

COMPARISON

JSON vs YAML vs XML โ€” Data Format Comparison

BEST OF

Best JSON Formatters Online 2026