cURL to Code Converter: how it works and how to build one
Convert cURL commands to fetch, axios, Python or PHP. This guide explains what happens behind the button and shows the code, so you can build your own.
What it does
Paste a cURL command copied from browser dev tools or API docs and get the same request in JavaScript fetch, Node axios, Python requests or PHP. Headers, methods, JSON bodies, form data and basic auth are supported.
When to use it
- Turning API docs examples into code
- Replaying a request from browser DevTools
- Sharing requests with teammates
How to use it
- Paste the cURL command.
- Pick the language.
- Copy the code.
Try it free in your browser, nothing is uploaded: https://eskillsfy.com/tools/curl-to-code
How it works
The converter first splits the cURL command into arguments like a shell would, respecting single and double quotes and line-continuation backslashes. Then it reads the flags it knows: -X for the method, -H for headers, -d for the body, -u for basic auth, -F for form fields.
With a simple request object built, each language is a template: fetch options, an axios config, a Python requests call, or PHP cURL options.
Build it yourself
Here is the core of the tool. It has no dependencies unless noted, so you can paste it into any TypeScript or JavaScript project.
curl.ts (ts)
export function tokenize(cmd: string) {
const s = cmd.replace(/\\\n/g, " ");
const out: string[] = []; let cur = "", quote: string | null = null;
for (const c of s) {
if (quote) { if (c === quote) quote = null; else cur += c; }
else if (c === "'" || c === '"') quote = c;
else if (/\s/.test(c)) { if (cur) out.push(cur); cur = ""; }
else cur += c;
}
if (cur) out.push(cur);
return out;
}
export function toFetch(cmd: string) {
const t = tokenize(cmd); let url = "", method = "GET", body: string | undefined; const headers: Record<string, string> = {};
for (let i = 1; i < t.length; i++) {
if (t[i] === "-X") method = t[++i];
else if (t[i] === "-H") { const [k, ...v] = t[++i].split(":"); headers[k.trim()] = v.join(":").trim(); }
else if (t[i] === "-d") { body = t[++i]; if (method === "GET") method = "POST"; }
else if (!t[i].startsWith("-")) url = t[i];
}
return `await fetch(${JSON.stringify(url)}, ${JSON.stringify({ method, headers, body }, null, 2)});`;
}
Make it production-ready
- Pretty-print JSON bodies.
- Support –data-urlencode, -u and -F.
Questions
Where do I get a cURL command?
In Chrome DevTools, right-click a request in the Network tab and choose Copy as cURL.
Free, runs in your browser, nothing is uploaded.