# How I convert a curl command into code I can actually use

A beginner-friendly guide to reading curl commands, understanding methods, headers, bodies, and converting them into fetch, Axios, or Python requests.

- Date: 2026-09-27
- URL: https://ilham.dev/posts/how-to-convert-curl-commands-into-code/
- Markdown: https://ilham.dev/posts/how-to-convert-curl-commands-into-code/index.md
- Tags: curl, api, http, tools
- Reading time: 2 min


API documentation often gives examples as `curl` commands:

```sh
curl -X POST https://api.example.com/users \
  -H "Authorization: Bearer TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"name":"Ana"}'
```

That is useful for testing, but eventually I need the same request in application
code.

The [curl Converter](/tools/curl-converter/) turns curl commands into JavaScript
`fetch`, Axios, or Python `requests` code.

## Step 1: read the URL

The URL is the endpoint being called:

```text
https://api.example.com/users
```

If the URL contains query parameters, keep them:

```text
https://api.example.com/users?active=true
```

A converted request should call the same URL.

## Step 2: read the method

This flag sets the HTTP method:

```sh
-X POST
```

Common methods:

- `GET`: read data;
- `POST`: create or submit data;
- `PUT`: replace data;
- `PATCH`: update part of data;
- `DELETE`: delete data.

If there is a request body with `-d`, curl often uses `POST` even without `-X POST`.
Still, I prefer making the method explicit in code.

## Step 3: read the headers

Headers use `-H`:

```sh
-H "Authorization: Bearer TOKEN"
-H "Content-Type: application/json"
```

In code, these become a headers object or dictionary.

For fetch:

```js
headers: {
  Authorization: 'Bearer TOKEN',
  'Content-Type': 'application/json'
}
```

Be careful with real tokens. Do not hard-code production secrets into frontend code.

## Step 4: read the body

The body often uses `-d`:

```sh
-d '{"name":"Ana"}'
```

For JSON APIs, code usually sends a JSON string.

Fetch example:

```js
body: JSON.stringify({ name: 'Ana' })
```

The `Content-Type: application/json` header tells the server how to read it.

## Step 5: paste into the converter

Open [curl Converter](/tools/curl-converter/), paste the command, and choose the
output language.

Then review the generated code. A converter can translate structure, but you still
need to decide where tokens, URLs, and variables should come from in your app.

## Step 6: replace example secrets with variables

Documentation often uses placeholders like:

```text
TOKEN
API_KEY
SECRET
```

In real code, replace those with configuration:

```js
const token = process.env.API_TOKEN;
```

Do not commit secrets to Git.

## Step 7: test the converted request

After converting, test with a safe environment:

- local server;
- sandbox API;
- staging environment;
- a test account.

Check:

- status code;
- response body;
- headers;
- error handling;
- timeout behaviour.

## My curl conversion checklist

When converting curl to code, I check:

1. Is the URL the same?
2. Is the HTTP method correct?
3. Are all headers included?
4. Is the body encoded correctly?
5. Are secrets replaced with variables?
6. Is this safe to run against the selected environment?
7. Did I add error handling after the basic request works?

The converter saves typing, but I still read the result. API calls are small, but
they often carry important credentials and side effects.
