cURL to axios Turn any curl command into an axios request for Node or the browser.
Turn any curl command into an axios request for Node or the browser.POSThttps://api.example.com/v1/customers
HEADERS · 2
AuthorizationBearer sk_t••••••→ API_TOKEN
Content-Typeapplication/json
BODY · JSON
{
"email": "ada@example.com",
"name": "Ada Lovelace",
"metadata": {
"plan": "pro",
"seats": 3
}
}axiosNode and browsers
import axios from "axios";
const { data } = await axios({
method: "post",
url: "https://api.example.com/v1/customers",
headers: {
Authorization: `Bearer ${process.env.API_TOKEN}`,
},
data: {
email: "ada@example.com",
name: "Ada Lovelace",
metadata: {
plan: "pro",
seats: 3,
},
},
});17 linesReads secrets from process.env
How curl maps to axios
The options you'll see in the code.
- paramsQuery strings move into a params object, so values are encoded for you.
- data-d and --json become data. axios turns objects into JSON and sets the header.
- auth-u user:pass becomes auth: { username, password } — no manual Base64.
- timeout-m 30 becomes timeout: 30000. axios counts in milliseconds.
Questions
The short answers.
axios or fetch?
fetch needs no install. axios gives you params, timeouts and errors on 4xx/5xx out of the box. Both are fine.
Does axios throw on errors?
Yes, for any status outside 2xx. Catch it and read error.response.status.
File uploads in Node?
Use the built-in FormData with a Blob in Node 18+, or the form-data package on older versions.
Why is there no Content-Type header for JSON?
axios sets application/json itself when data is an object, so the header is left out to keep the code short.
Are large numbers in a JSON body kept?
Yes. If a number is too big for a JavaScript number, the body is sent as the original JSON text with its Content-Type header, so no digits are lost.