What Is curl? Basic Flags and Converting a curl Command to fetch
Updated: October 13, 2026 · AracMerkezi
▶ Open cURL to Fetch Converter
curl is a widely used tool for sending HTTP requests from the command line. API documentation almost always shows example requests with curl. When you want to run these commands with fetch in browser or Node.js code, knowing how to translate them saves time.
The simplest request
curl https://api.example.com/users
By default it sends a GET request and prints the response body to the screen.
Commonly used flags
| Flag | Meaning |
|---|---|
-X POST | Specifies the HTTP method |
-H "Header: value" | Adds a request header |
-d 'data' | Sends the request body (makes the method POST by default) |
-i | Also shows the response headers |
-I | Requests only the headers (HEAD) |
-L | Follows redirects |
-u username:password | Basic authentication |
-s | Silent mode (hides the progress bar) |
POST example
curl -X POST https://api.example.com/users \
-H "Content-Type: application/json" \
-H "Authorization: Bearer TOKEN" \
-d '{"name":"Ayşe","age":30}'
The backslash at the end of a line indicates that the command continues (it is different in the Windows command prompt).
The fetch equivalent
The request above is written in JavaScript like this:
fetch("https://api.example.com/users", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer TOKEN"
},
body: JSON.stringify({ name: "Ayşe", age: 30 })
})
.then(r => r.json())
.then(console.log);
The mapping is simple: -X → method, -H → headers, -d → body. If the body is JSON, remember to convert the object to a string with JSON.stringify. The cURL to Fetch Converter translates common flags like this automatically.
What differs in the browser: CORS
A request that works in curl can be blocked in the browser because of CORS. For requests to an API on a different domain, the browser looks for the server's permission headers. curl does not perform this check. If you get a CORS error, the problem is not in your code but in the server's permission settings.
Reading the response
You can tell success from failure by the HTTP status code: 200 is success, 401 unauthorized, 404 not found, 500 server error. You can look up their meanings in the HTTP Status Codes Reference tool and in the HTTP status codes guide. Except for network errors, fetch does not throw an error even on 404 or 500; check the response.ok value.
Security tips
- Remove tokens and passwords from commands when sharing them; they are also stored in your shell history.
- Do not embed secret keys in client-side code.
- If the URL has parameters, inspect its parts with the URL Parser.
Step by step: running a curl example from API documentation
- Identify the URL and the method in the example.
- Write the headers (
-H) into an object. - Convert the body in
-dinto a JavaScript object and send it withJSON.stringify. - Check the status code and the body of the response.
- Move the token out of the code and into an environment variable.
GET and query parameters
In the request curl "https://api.example.com/search?q=cat&page=2", the URL must be in quotes; otherwise the shell treats the & character as a command separator. On the fetch side, building the URL with new URL and searchParams ensures that special characters are encoded correctly.
Sending form data
A body in the form -d "name=Ayşe&age=30" is sent as application/x-www-form-urlencoded. In fetch, you get the same result if you pass a URLSearchParams object as the body. To send JSON, you need to specify Content-Type: application/json in the header; otherwise the server may not be able to read the body.
Debugging
- Look at the response headers with
curl -i. -v(verbose) shows connection and header information.- If you get a 301/302, use
-L. - If you get a 401, check the
Authorizationheader and the token's expiration. - Before moving the request to a tool like Postman, verify it with curl in its simplest form.
Limits
The converter covers common flags (method, header, body, basic authentication). Advanced options such as file uploads, cookie files, proxies and client certificates must be adapted by hand.
Frequently asked questions
Which HTTP method does curl -d use?
-d sends a body and makes the method POST by default.
Why does a request that works in curl fail in the browser?
Most often because of CORS; the browser looks for the server's permission headers, while curl does not check for them.
Does fetch throw an error on a 404?
No; it rejects only on a network error. You need to check response.ok.
Tools mentioned in this guide
More guides
- What Are HTTP Status Codes? 200, 301, 404, 500 and Troubleshooting – What do HTTP status codes mean? Codes 200, 301, 404, 500, testing with curl, the cost of redirect chains, soft 404s and site-migration examples.
- What Are JSON, Base64 and URL Encoding? Encoding vs Encryption vs Hashing – What do JSON, Base64, URL encoding, HTML entities, SHA and JWT do? Encoding vs encryption vs hashing, with examples that were run and verified.
- What Is JSON? Structure, Formatting, Validation and Finding Errors – What is JSON and how do you read and validate it? A nested example, a valid/invalid comparison table, number and date traps and the formatting difference.
- What Is Regex (a Regular Expression)? A Beginner's Guide with Tested Examples and Common Mistakes – What is regex, and what do \d \w + * [] () mean? Date, phone and email examples, greedy matching, the Turkish letter issue and a ReDoS warning.
- What Is chmod? Linux File Permissions 755, 644 and 777 Explained: Reading ls -l, umask and Examples – What is chmod? Read ls -l output, understand 755 644 600 777, symbolic mode, umask and special bits. Commands were run and verified.
This article is for general information only and is not legal, financial or medical advice. We are not an official institution.