cURL to Code: Convert API Requests Between Languages Instantly
Understand cURL's flag syntax, common REST API patterns, and how to accurately translate cURL commands into JavaScript fetch, Python requests, and Node.js axios — including the browser DevTools 'Copy as cURL' workflow.
Every API has documentation. Almost universally, that documentation includes code examples in cURL — the command-line HTTP client that ships on every Unix-like system and has been the lingua franca of API documentation for decades. The problem is that you are not writing shell scripts. You are writing JavaScript, Python, Go, or Ruby, and you need to translate that cURL command into working code before you can use it.
That translation is tedious and error-prone. Headers, authentication schemes, request bodies, and URL encoding all have to be mapped to the right method calls in the right language. The BrowseryTools cURL Converter does this automatically — paste a cURL command and get equivalent code in JavaScript fetch, Python requests, Node.js axios, and more. Free, no sign-up, everything stays in your browser.
What Is cURL?
cURL (Client URL) is a command-line tool for transferring data using URLs. It supports HTTP, HTTPS, FTP, WebSockets, and dozens of other protocols. For developers, it is most commonly used as a way to make HTTP requests from the terminal — testing an API endpoint, downloading a file, or debugging authentication.
cURL is installed by default on macOS and most Linux distributions. On Windows, it has been bundled with the OS since Windows 10. This ubiquity is exactly why API documentation teams default to cURL for examples — they can be confident that any developer reading the docs can run the example immediately, without installing anything.
Anatomy of a cURL Command
A cURL command is built from a base URL and a set of flags. Here is a complete example that covers the most important flags:
curl -X POST https://api.example.com/v1/users \
-H "Content-Type: application/json" \
-H "Authorization: Bearer eyJhbGciOiJIUzI1NiJ9..." \
-d '{"name": "Alice", "email": "alice@example.com"}'Breaking down each flag:
-X POST— sets the HTTP method. Valid values are GET, POST, PUT, PATCH, DELETE, etc. If omitted and-dis present, cURL defaults to POST.-H "Header: Value"— adds a request header. Can be repeated multiple times for multiple headers.-d '...'— the request body. For JSON, combine with-H "Content-Type: application/json". cURL URL-encodes the body by default unless you use--data-raw.--data-raw '...'— sends the body exactly as-is without any URL encoding. Required when the body contains characters like@that-dwould interpret specially.-u username:password— basic authentication shorthand. cURL encodes it as a Base64 Authorization header for you.-s— silent mode; suppresses the progress bar. Nearly always used in scripts.-v— verbose mode; prints request and response headers. Invaluable for debugging authentication failures.-o filename— write output to a file instead of stdout.
Common cURL Patterns for REST APIs
GET Request with Query Parameters
curl "https://api.example.com/users?page=2&limit=20" \ -H "Authorization: Bearer TOKEN"
Query parameters go directly in the URL. Quote the entire URL to prevent the shell from interpreting the & as a background process operator.
POST with JSON Body
curl -X POST https://api.example.com/orders \
-H "Content-Type: application/json" \
-H "X-API-Key: your-api-key" \
--data-raw '{"product_id": 42, "quantity": 3}'File Upload (multipart/form-data)
curl -X POST https://api.example.com/upload \ -H "Authorization: Bearer TOKEN" \ -F "file=@/path/to/document.pdf" \ -F "description=Q4 Report"
The -F flag sends multipart/form-data. The @ prefix means "read from file". This is the format used for image uploads, document processing APIs, and any endpoint that accepts binary data.
Converting cURL to JavaScript fetch
// Original cURL:
// curl -X POST https://api.example.com/v1/users \
// -H "Content-Type: application/json" \
// -H "Authorization: Bearer TOKEN" \
// -d '{"name": "Alice", "email": "alice@example.com"}'
const response = await fetch("https://api.example.com/v1/users", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer TOKEN",
},
body: JSON.stringify({
name: "Alice",
email: "alice@example.com",
}),
});
const data = await response.json();Converting cURL to Python requests
import requests
response = requests.post(
"https://api.example.com/v1/users",
headers={
"Authorization": "Bearer TOKEN",
},
json={
"name": "Alice",
"email": "alice@example.com",
},
)
data = response.json()The requests library's json= parameter handles both serialization and setting the Content-Type: application/json header automatically — no need to set it manually.
Converting cURL to Node.js with axios
const axios = require("axios");
const response = await axios.post(
"https://api.example.com/v1/users",
{
name: "Alice",
email: "alice@example.com",
},
{
headers: {
Authorization: "Bearer TOKEN",
},
}
);
const data = response.data;How "Copy as cURL" Works in Browser DevTools
One of the most useful features in browser DevTools is "Copy as cURL." In Chrome, Firefox, or Safari: open DevTools, go to the Network tab, make a request (log in, click a button, load a page), right-click the request in the network list, and select "Copy as cURL."
The browser generates a complete cURL command that includes every header the browser sent — including cookies, session tokens, CSRF tokens, and any other authentication material. This means you can replay the exact request that the browser made, including all its authentication context, from the terminal or from code.
This is invaluable for debugging: if the browser request works but your code's request fails, paste both into a diff and find the header or body difference. You can also paste the copied cURL directly into the BrowseryTools cURL Converter to get equivalent code in your preferred language — the converter handles all the escaping, quoting, and flag translation automatically.
Summary
cURL is the universal language of HTTP. API docs use it because everyone can run it. DevTools copies it because it captures every detail of a request. Learning to read cURL fluently — and to translate it accurately to whatever language you are working in — is a practical skill that pays dividends every time you integrate a new API. Skip the tedious manual translation and use the BrowseryTools cURL Converter to get clean, runnable code in seconds.
Try the Tools — 100% Free, No Sign-Up
Everything runs in your browser. No uploads. No accounts. No ads.
Explore All Tools →