Skip to content

How to Convert Curl Commands to Code: JavaScript, Python and More

Every developer has copied a curl command from API documentation and wondered how to turn it into working code. Curl is the universal language of HTTP examples. It appears in nearly every REST API reference, Stripe docs, Twilio guides and AWS documentation. But your application does not run curl commands directly. You need to translate those examples into JavaScript, Python, Go or whatever language your project uses.

What is Curl?

Curl (short for "Client URL") is a command-line tool for making HTTP requests. It has been around since 1998 and is installed by default on macOS, Linux and modern Windows systems. Curl supports virtually every HTTP feature: custom headers, request bodies, authentication, cookies, redirects and more. Its ubiquity is why API documentation uses curl examples as the common denominator.

A typical curl command looks like this:

curl -X POST https://api.example.com/users \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer tok_abc123" \
  -d '{"name": "Alice", "email": "alice@example.com"}'

Understanding Common Curl Flags

Before converting curl commands, you need to understand what each flag does. Here are the flags you will encounter most often:

  • -X or --request: Sets the HTTP method (GET, POST, PUT, DELETE, PATCH). If omitted, curl defaults to GET.
  • -H or --header: Adds an HTTP header. You can use multiple -H flags for multiple headers.
  • -d or --data: Sends data in the request body. When used, curl automatically sets the method to POST unless overridden.
  • -u or --user: Sets Basic authentication credentials in the format username:password.
  • -F or --form: Sends data as multipart form data, used for file uploads.
  • -L or --location: Follows HTTP redirects automatically.
  • -k or --insecure: Skips SSL certificate verification (use only for local development).

Converting Curl to JavaScript Fetch

The Fetch API is the standard way to make HTTP requests in modern JavaScript. Here is how the curl example above translates to fetch:

const response = await fetch("https://api.example.com/users", {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    "Authorization": "Bearer tok_abc123",
  },
  body: JSON.stringify({
    name: "Alice",
    email: "alice@example.com",
  }),
});

const data = await response.json();

The mapping is straightforward. The -X POST becomes method: "POST", each -H flag becomes an entry in the headers object, and the -d data becomes the body property. Note that fetch does not follow redirects for POST requests by default and does not throw on HTTP error status codes, so you should always check response.ok.

Converting Curl to Python Requests

Python's requests library is the most popular HTTP client in the Python ecosystem. The same curl command converts to:

import requests

response = requests.post(
    "https://api.example.com/users",
    headers={
        "Content-Type": "application/json",
        "Authorization": "Bearer tok_abc123",
    },
    json={
        "name": "Alice",
        "email": "alice@example.com",
    },
)

data = response.json()

When using the json parameter instead of data, the requests library automatically sets the Content-Type header to application/json and serializes the dictionary. For Basic auth via -u, use the auth=("user", "pass") parameter.

Converting Curl to Go

Go's standard library provides everything you need for HTTP requests without external dependencies:

body := strings.NewReader(`{"name":"Alice","email":"alice@example.com"}`)
req, err := http.NewRequest("POST", "https://api.example.com/users", body)
if err != nil {
    log.Fatal(err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer tok_abc123")

resp, err := http.DefaultClient.Do(req)
if err != nil {
    log.Fatal(err)
}
defer resp.Body.Close()

Handling Authentication

Curl commands in API docs commonly use two authentication patterns. Bearer tokens appear as -H "Authorization: Bearer TOKEN" and translate directly to a header in any language. Basic auth uses -u user:pass, which curl converts to a Base64-encoded Authorization: Basic header automatically. In your code, most HTTP libraries have a dedicated auth parameter. In fetch, you need to construct the header manually:

// Basic auth in fetch
const credentials = btoa("username:password");
headers: {
  "Authorization": `Basic ${credentials}`
}

Common Conversion Pitfalls

  • Forgetting to parse JSON. Curl prints the raw response. In code, you need to explicitly parse JSON with response.json() or equivalent.
  • Ignoring error handling. Curl exits with a non-zero code on connection failure, but HTTP 4xx/5xx responses are still "successful" connections. Your code must check the status code.
  • Mixing up data and json parameters. In Python requests, data sends form-encoded data while json sends JSON. Match the Content-Type from the curl command.
  • Single vs double quotes. Curl commands from documentation often use single quotes, which do not work in Windows cmd. Your code should use your language's native string syntax instead.
  • Missing redirect handling. Curl with -L follows redirects. In fetch, set redirect: "follow". In Python requests, redirects are followed by default.

Real-World Example: Stripe API

Stripe's documentation provides curl examples for every endpoint. Here is a typical charge creation command and its JavaScript equivalent:

// Curl
curl https://api.stripe.com/v1/charges \
  -u sk_test_xxx: \
  -d amount=2000 \
  -d currency=usd

// JavaScript fetch equivalent
const response = await fetch("https://api.stripe.com/v1/charges", {
  method: "POST",
  headers: {
    "Authorization": "Basic " + btoa("sk_test_xxx:"),
    "Content-Type": "application/x-www-form-urlencoded",
  },
  body: "amount=2000&currency=usd",
});

Notice that Stripe uses form-encoded data (not JSON) and Basic auth with just the API key and no password. These details are easy to miss when converting manually, which is why an automated converter can save time and prevent bugs.

Convert curl commands instantly

Paste any curl command and get working code in JavaScript, Python, Go and more. No signup required.

Open Curl to Code Converter