Calcro
Developer Tools

cURL to Code Converter

Transform cURL commands into clean, production-ready code in Python Requests, JavaScript (Fetch), Node.js (Axios), PHP (Guzzle), and Go (net/http). Features automatic JSON formatting, shell escape sanitation, and 100% client-side execution.

Loading cURL Converter...

How cURL Commands Translate Across Programming Languages

cURL (Client URL) is the universal command-line standard for testing APIs and inspecting HTTP transactions. However, integrating raw cURL snippets from API documentation into an active application codebase requires translating flags, headers, authentication mechanisms, and payloads into language-specific HTTP client libraries.

Each programming language runtime has its own idioms for connection pooling, header formatting, payload serialization, and error handling. Understanding how cURL flags map to native constructs in Python, JavaScript, PHP, and Go eliminates bugs and security vulnerabilities such as header injection or improperly delimited multipart forms.

cURL Flags to Code Translation Reference

The table below illustrates how common cURL command-line arguments map directly to properties and methods in popular HTTP client runtimes:

cURL FlagHTTP ConceptPython (Requests)JavaScript (Fetch)Node.js (Axios)Go (net/http)
-X POSTHTTP Methodrequests.post()method: 'POST'method: 'post'http.NewRequestWithContext
-H 'Key: Val'Custom Headerheaders={'Key': 'Val'}headers: {'Key': 'Val'}headers: {'Key': 'Val'}req.Header.Set("Key", "Val")
-u user:passBasic Authauth=('user', 'pass')Authorization: 'Basic ' + btoa()auth: {'username', 'password'}req.SetBasicAuth("user", "pass")
-d '{...}'JSON Payloadjson={'key': 'val'}body: JSON.stringify()data: {...}bytes.NewBuffer(jsonData)
--data-urlencodeForm Datadata={'key': 'val'}body: new URLSearchParams()data: params.toString()formData.Encode()
-F file=@pathMultipart / Filesfiles={'file': open()}body: new FormData()FormData (form-data package)multipart.Writer
-k, --insecureSkip SSL Verifyverify=Falseagent: new https.Agent()httpsAgent: {'rejectUnauthorized': false}InsecureSkipVerify: true
-m, --max-timeRequest Timeouttimeout=30AbortSignal.timeout(30000)timeout: 30000Timeout: 30 * time.Second

Deep Dive: Authentication Patterns

Authentication headers must be handled carefully when translating from cURL. There are two dominant authentication formats:

1. Bearer Token Authentication (OAuth 2.0 / JWT)

A Bearer token is transmitted via the Authorization: Bearer <token> header. Across all languages, the token should be kept securely in environment variables (such as process.env.API_KEY or os.environ.get("API_KEY")).

In JavaScript Fetch, ensure that the header key is properly capitalized:

const response = await fetch('https://api.example.com/v1/resource', {
  method: 'GET',
  headers: {
    'Authorization': `Bearer ${process.env.API_SECRET_TOKEN}`,
    'Accept': 'application/json'
  }
});

2. HTTP Basic Authentication

cURL's -u username:password flag base64-encodes the credentials and prefixes them with Basic in the Authorization header. Modern clients like Python Requests and Axios provide first-class authentication tuples or config objects that automatically handle encoding:

# Python Requests handles Base64 encoding transparently
response = requests.get(
    'https://api.example.com/v2/secure-data',
    auth=('my_api_key', 'my_api_secret')
)

Payload Formats: JSON vs. URL-Encoded vs. Multipart Form Data

One of the most frequent sources of runtime errors when translating cURL commands is confusing payload serialization:

  • JSON Payloads (-d / --data-raw): When the payload is a valid JSON string, Python Requests provides the json=... parameter, which automatically serializes the Python dictionary and sets Content-Type: application/json. In Fetch, you must explicitly call JSON.stringify() and include the Content-Type: application/json header.
  • URL-Encoded Form Data (--data-urlencode): Used for HTML form submissions and OAuth token exchanges. Translates to data=dict in Python and new URLSearchParams() in JavaScript. The client sets Content-Type: application/x-www-form-urlencoded.
  • Multipart Form-Data (-F / --form): Required when uploading binary files alongside metadata fields. Critical Rule: Never manually set the Content-Type header for multipart requests. The HTTP library must generate a unique boundary string (e.g., multipart/form-data; boundary=---------------------------974767299852498929531610575) and calculate the byte offsets automatically.

Handling Line Breaks across Operating Systems

Developer documentation frequently formats long cURL commands across multiple lines using the Unix shell continuation character (\). When copied into Windows terminals (cmd.exe or PowerShell), these commands fail with syntax errors because Windows CMD expects a caret (^) and PowerShell expects a backtick (`).

Calcro's cURL to Code Converter sanitizes these escape characters automatically on paste and provides anAuto-Clean utility to normalize broken terminal input instantly.

Frequently Asked Questions

To convert cURL to Python Requests, map `-X METHOD` to `requests.method()`, `-H 'Header: Value'` to a `headers={'Header': 'Value'}` dictionary, and `-d` JSON data to the `json=data` parameter (or `data=data` for form-encoded strings). If basic authentication `-u user:pass` is present, pass `auth=('user', 'pass')`. Our converter generates ready-to-run Python code with appropriate exception handling and response deserialization.

The `-d` (or `--data`) flag sends the raw string as the HTTP request body without modification (or URL-encodes only if manually formatted). In contrast, `--data-urlencode` automatically URL-encodes special characters (such as spaces, ampersands, and equal signs) in the provided value. When converting to JavaScript Fetch or Python Requests, `--data-urlencode` translates to `new URLSearchParams()` or a dictionary passed to `data=`, whereas raw `-d` JSON strings translate to `json=payload` or `JSON.stringify()`.

In JavaScript Fetch, a Bearer token is passed inside the `headers` object as `'Authorization': 'Bearer YOUR_TOKEN'`. Set the HTTP method in the options object and serialize your payload with `JSON.stringify()`. You then call `await fetch(url, options)` and parse the response with `await response.json()`. Our tool formats this pattern into modern, async/await JavaScript.

In cURL, multipart form submissions use `-F field=value` and `-F 'file=@/path/to/file'`. When converting to Python Requests, fields and files are organized into a `files` dictionary: regular fields use `('field', (None, 'value'))` and file attachments use `('file', open('filename', 'rb'))`. Crucially, you do NOT manually set the `Content-Type: multipart/form-data` header in Python — the `requests` library automatically generates the multipart boundary for you.

In Go's `net/http`, basic authentication is applied to the request using `req.SetBasicAuth(username, password)`. In Node.js Axios, it is passed via the config object's `auth: { username, password }` property. In standard JavaScript Fetch, you encode the credentials using Base64: `'Authorization': 'Basic ' + btoa('user:password')`.

Unix shells (bash, zsh) use a trailing backslash `\` to continue a command onto the next line, while Windows Command Prompt (CMD) uses a caret `^` and PowerShell uses a backtick `` ` ``. If you copy a multiline command across operating systems or with trailing spaces after the escape character, the shell fails to parse the arguments. Calcro's cURL Converter features an 'Auto-Clean' engine that automatically detects and strips Unix backslashes, Windows carets, and command prompt symbols ($ or >) to produce valid single-string commands.

No. All parsing and code generation is performed 100% locally in your web browser using client-side JavaScript. Your cURL commands, private headers, Bearer tokens, passwords, and request payloads never leave your computer or hit any external server.