hackandhost

Blog · · 3 min read

curl to fetch, axios and Python

Paste a curl command, or one copied from your browser's dev tools, and get the same request as JavaScript fetch, axios or Python requests code.

API docs show examples as curl commands, and browser dev tools let you copy any request as one. Turning that into code by hand means getting every header, quote and body field right. Paste a curl command below and get the same request as fetch, axios or Python requests. The conversion runs in your browser, so tokens and cookies in the command stay on your machine.

Converted in your browser. Commands often contain tokens and cookies; nothing is sent anywhere or saved.

fetch
const response = await fetch("https://api.example.com/v1/posts?draft=false", {
  method: "POST",
  headers: {
    "accept": "application/json",
    "authorization": "Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1c2VyXzU1MSJ9.c2lnbmF0dXJl",
    "content-type": "application/json",
  },
  body: JSON.stringify({
    title: "Hello from curl",
    tags: [
      "cron",
      "jwt",
    ],
    published: true,
    coverImage: null,
  }),
});

if (!response.ok) throw new Error(`HTTP ${response.status}`);
const data = await response.json();
console.log(data);
What this request does
MethodPOST
URLhttps://api.example.com/v1/posts
Query
draft = false
Headers
accept: application/json
authorization: Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1c2VyXzU1MSJ9.c2lnbmF0dXJl
content-type: application/json
Removedsec-ch-ua-platform, sec-fetch-mode, accept-language
(added by browsers; not needed in code)
BodyJSON
{
  "title": "Hello from curl",
  "tags": [
    "cron",
    "jwt"
  ],
  "published": true,
  "coverImage": null
}
RedirectsFollowed (-L)

Copying a request from your browser

Every major browser can copy a request it made as curl:

  1. Open dev tools (F12) and go to the Network tab.
  2. Do the thing in the page that makes the request, and click it in the list.
  3. Right-click it and choose Copy → Copy as cURL. On Windows, Chrome and Firefox offer a Windows version and a bash or POSIX version; either works here.

Browsers add a lot of headers your code doesn't need, such as sec-ch-ua, sec-fetch-mode and accept-language. Remove browser-only headers strips them out; untick it to keep everything.

A copied command usually includes your session cookie or an Authorization token. Treat it like a password: don't paste it into chat, issues or tools that send it to a server.

What the common flags become

curl fetch axios requests
-X POST method: "POST" method: "post" requests.post(…)
-H 'Name: value' headers: {…} headers: {…} headers={…}
-d / --data-raw with JSON body: JSON.stringify(…) data: {…} json=…
-d 'a=1&b=2' body: new URLSearchParams(…) data: new URLSearchParams(…) data={…}
-F 'file=@photo.jpg' FormData FormData files={…}
-u user:pass Authorization: Basic … auth: {…} auth=(…)
-b 'session=abc' Cookie header Cookie header cookies={…}
-m 10 AbortSignal.timeout(10000) timeout: 10000 timeout=10
-k not possible per request httpsAgent verify=False

Flags that only change what curl prints, like -s, -v, -i and -o, have no equivalent and are skipped.

Differences that catch people out

  • -d sends form data unless you say otherwise. curl -d '{"a":1}' labels the body application/x-www-form-urlencoded, not JSON, and many APIs reject it. Add -H 'Content-Type: application/json', or use --json, which sets both Content-Type and Accept to application/json. The tool warns when it sees a JSON body sent as form data.
  • Redirects. curl only follows redirects with -L. fetch, axios and requests follow them by default, so the code may end up at a different URL than the command did.
  • Browsers ignore some headers in fetch. Code running in a web page can't set Cookie, Referer or Origin; the browser handles those itself. The same code run in Node 18 or later sends them.
  • -k turns off certificate checks. It's fine against a local server with a self-signed certificate, but never in production code. The generated code keeps a comment where it applies.
  • -G moves the -d data into the URL's query string and sends a GET, which is easy to miss in a long command.

If a flag can't be converted, such as a proxy, a cookie file or headers read from a file, the tool lists it under Check these instead of dropping it silently.