cURL to code

Paste a cURL command, such as the one a browser copies from the network tab, and get the same request as fetch, axios, Node, Python, Go, PHP, Java or

loading cURL to code
About this tool

What is a cURL to code converter?

cURL is the command line tool everyone uses to try an HTTP request. API documentation is full of curl examples, and your browser can copy any request from the network tab as a curl command. A cURL to code converter turns that one line into the same request written in the language of your project, so you do not have to translate headers, quotes and body by hand.

You paste a command and pick a target: JavaScript with fetch, JavaScript with axios, Node.js with the http and https modules, Python with requests, Go with net/http, PHP with cURL, Java with HttpClient or C# with HttpClient. The tool reads the method, the URL, the headers, the body, basic authentication, cookies, form fields and file uploads, and writes code that sends the same thing.

Two things are worth knowing. First, the command is only read as text. Nothing is run and nothing is sent to the API, so it is safe to paste a command that contains a token, although you should not share the result with anyone who should not see it. Second, the tool does not execute the request for you: it gives you code to run in your own environment. Everything happens in your browser.

How to convert a cURL command

  1. In the browser, open the network tab, right-click a request and choose "Copy as cURL". Or copy the example from the API documentation. Commands copied from Windows cmd or PowerShell work too.
  2. Paste it in the input box. Multi-line commands with \ at the end of each line are fine.
  3. Choose the target language. The code appears on the right.
  4. Look at the note below the code. It lists the curl options that were not carried over, for example --retry.
  5. Copy the code or download it as a file.
  6. Replace the token or password with a variable before you commit the code anywhere.

Examples

A JSON POST with a bearer token

Command:

curl -X POST 'https://api.example.com/v1/orders?expand=items' \
  -H 'Authorization: Bearer sk_test_abc' \
  -H 'Content-Type: application/json' \
  -d '{"customer":"ana@example.com","qty":2}'

As fetch:

const response = await fetch("https://api.example.com/v1/orders?expand=items", {
  method: "POST",
  headers: {
    "Authorization": "Bearer sk_test_abc",
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    "customer": "ana@example.com",
    "qty": 2
  }),
  redirect: "manual",
});

console.log(response.status);
console.log(await response.text());

As Python with requests:

import requests

headers = {
    "Authorization": "Bearer sk_test_abc",
    "Content-Type": "application/json",
}

payload = {
    "customer": "ana@example.com",
    "qty": 2,
}
response = requests.request(
    "POST",
    "https://api.example.com/v1/orders?expand=items",
    headers=headers,
    json=payload,
    allow_redirects=False,
)

print(response.status_code)
print(response.text)

Notice redirect: "manual" and allow_redirects=False. curl does not follow redirects unless you pass -L, so the generated code does not either, to behave the same way.

A file upload with basic authentication

Command:

curl -u ana:s3cret -F 'file=@a.png' -F name=x -L https://x.io/up

As Python:

import requests

data = {
    "name": "x",
}

files = {
    "file": open("a.png", "rb"),
}
response = requests.request(
    "POST",
    "https://x.io/up",
    data=data,
    files=files,
    auth=("ana", "s3cret"),
    allow_redirects=True,
)

print(response.status_code)
print(response.text)

The -F options make the method POST and the body multipart/form-data. -L turns the redirects on.

Options that cannot be carried over

Command:

curl --retry 3 -m 5 -k 'https://api.x.io/items?id=1' -H 'Accept: application/json'

The code for axios sends a GET with the Accept header and turns off certificate checks for -k. The note under the code says that --retry and --max-time were not carried over, because they describe how curl behaves, not what the request is. Add a timeout and a retry policy with your own HTTP library.

Common mistakes

  • Pasting a command cut in the middle. An unclosed quote is reported with the quote character. Copy the whole command, including the last line.
  • A header without a colon. -H 'X' is not a header. The tool says which header is wrong and shows the form -H "Name: value".
  • A form field without an equals sign. -F needs name=value or name=@file.
  • Forgetting that -d changes the method. With a body and no -X, curl sends POST, and so does the generated code. With -G the data goes in the query string instead.
  • Leaving the secret in the code. The token in the command is copied as it is. Move it to an environment variable before you save the file.
  • Expecting the same behavior as curl in every detail. Proxies, client certificates, retries and timeouts are not translated. Check the note under the output.

Frequently asked questions

Does the tool call my API?

No. It only reads the text of the command and writes code. Nothing is sent anywhere, and the request is never made.

Can I use a command copied from Chrome or Firefox?

Yes. "Copy as cURL" from the network tab works, including the long list of headers. You can remove the ones you do not need, such as the browser cookies, in the generated code.

How do I format the JSON body or build the Authorization header?

Use JSON prettify for the body, and the Basic auth generator when you need to build the header by hand. The query string can be read with the URL parser.

Why does the code not follow redirects?

Because curl does not follow them unless you pass -L. Add -L to your command and the generated code follows them.

Report a problem