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.

Updated 2026-06-28

API documentation overwhelmingly shows example requests as cURL commands, but actually using that API in your application means translating the command into your language's HTTP client syntax. This guide walks through that conversion manually, then shows how a converter tool automates the same process.

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 โ€” this is 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), replace it with 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 reflects this.

Mishandling multi-line commands. Trailing backslashes indicate line continuations; don't treat each line as a separate command.

Leaving real credentials in shared code. Always scrub API keys and tokens before committing or sharing converted code.

Missing quoted string escaping. JSON bodies inside single-quoted shell strings can contain escaped characters that need careful handling when reconstructing 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

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.
cURL itself defaults to a GET request unless a request body is provided via -d or --data, in which case it automatically switches to POST. Any code converter should replicate this same default behaviour rather than always assuming GET.
Multi-line cURL commands use a trailing backslash (\) at the end of each line to indicate the command continues on the next line. A correct parser treats these as line continuations and reconstructs the full single command before processing it โ€” paste the command exactly as copied, backslashes included.
Python output typically uses the `requests` library, JavaScript output uses the browser-native `fetch` API, and Node.js output uses the `axios` library โ€” three of the most common ways developers make HTTP requests in each environment. Pick whichever matches the environment you're actually working in.
Python's `requests` and Node.js's `axios` are both external packages requiring installation (`pip install requests` or `npm install axios`), while JavaScript's `fetch` works natively in modern browsers and recent Node.js versions without any extra installation.
Each -H or --header flag in the cURL command becomes a key-value entry in the target language's headers object or dictionary, preserving the exact header name and value from the original command.
Basic authentication credentials passed via -u user:pass are translated into the target language's equivalent mechanism โ€” a tuple passed to the auth parameter in Python's requests, or an auth object with username/password fields in axios.
Yes โ€” if your original cURL command included a real API key, bearer token, or password, the converted code will contain that same credential. Always replace real credentials with environment variables or placeholders before committing the code or sharing it with anyone.
Basic conversion tools typically handle headers, body data, and authentication well, but more advanced cURL flags like file uploads (-F) often need manual translation, since multipart form handling differs significantly between languages and libraries.
Yes โ€” since the underlying request (URL, method, headers, body) is the same regardless of target language, you can convert the same cURL command to Python, JavaScript, and Node.js separately to get equivalent code in each, useful when you need the same API call available across 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