Most curl questions come down to one thing: what did the server actually receive? A request that “should” be JSON arrives labeled as a form, a POST silently turns into a GET after a redirect, a 404 page sails through a script as if it were a success. To make that visible, a tiny test server was written that answers every request with a description of it: the method, the path, the headers that matter and the exact body. Every example below was sent to it with curl 8.5.0, the version in Ubuntu 24.04.
A plain request
GET
$ curl -s http://127.0.0.1:8765/echo
{"method": "GET", "path": "/echo", "accept": "*/*", "user_agent": "curl/8.5.0"}
With no options, curl sends a GET, announces itself as curl/8.5.0, and accepts any content type. -s (silent) hides the progress meter, which otherwise clutters output in scripts.
Headers only
curl -sI sends a HEAD request and prints just the response headers, such as HTTP/1.0 200 OK, the server name and the date, which is the quickest way to check a status code, a redirect target or a cache header without downloading a page. -i prints the headers followed by the body of a normal request.

Sending headers
Custom headers and the user agent
$ curl -s -H 'X-Api-Key: abc123' -H 'User-Agent: my-script/1.0' http://127.0.0.1:8765/echo
{"method": "GET", "path": "/echo", "accept": "*/*", "user_agent": "my-script/1.0", "x_api_key": "abc123"}
Each -H adds a header, and supplying one curl normally sends, such as User-Agent, replaces curl’s own. A recognizable user agent is polite to API operators and makes a script’s traffic easy to find in their logs.
Basic authentication, and what it really sends
$ curl -s -u alice:s3cret http://127.0.0.1:8765/private
{... "auth": "Basic YWxpY2U6czNjcmV0" ...}
$ echo YWxpY2U6czNjcmV0 | base64 -d
alice:s3cret
The Authorization header was just the username and password in base64, which anyone can decode, as the second command did. Basic authentication is only private over HTTPS. Putting a password directly on the command line also leaves it in shell history and, while the command runs, visible in the process list; -u alice alone makes curl prompt for it instead.
POST data and the JSON trap
-d sends a form
$ curl -s -d 'name=Ana&qty=2' http://127.0.0.1:8765/orders
{"method": "POST", ..., "content_type": "application/x-www-form-urlencoded", "body": "name=Ana&qty=2"}
-d switched the method to POST on its own. The curl manual for Ubuntu 24.04 says -d passes data using the content type application/x-www-form-urlencoded, the same way a browser submits a form.
JSON sent with -d is still labeled a form
$ curl -s -d '{"name":"Ana"}' http://127.0.0.1:8765/orders
{"method": "POST", ..., "content_type": "application/x-www-form-urlencoded", "body": "{\"name\":\"Ana\"}"}
The body was JSON; the label said it was a form. Many APIs reject that with a 400 or 415 error, or parse it as an empty form and complain that a required field is missing, which sends people hunting for a bug in their JSON when the real problem is one header.
Two fixes
$ curl -s -H 'Content-Type: application/json' -d '{"name":"Ana"}' http://127.0.0.1:8765/orders
{..., "content_type": "application/json", "accept": "*/*", ...}
$ curl -s --json '{"name":"Ana"}' http://127.0.0.1:8765/orders
{..., "content_type": "application/json", "accept": "application/json", ...}
The manual describes --json as a shortcut that sends the data as a POST and sets both Content-Type and Accept to application/json. The echo confirmed both headers. It also reads files: curl --json @order.json sent the file’s contents unchanged, trailing newline included.
Encoding form values safely
$ curl -s --data-urlencode 'note=fish & chips 100%' http://127.0.0.1:8765/notes
{..., "body": "note=fish+%26+chips+100%25"}
With plain -d, the & would have split the value into two fields and the % would have been read as the start of an escape. --data-urlencode encoded both.

When -X changes less than expected
GET with a body
$ curl -s -X GET -d 'q=1' http://127.0.0.1:8765/search
{"method": "GET", ..., "content_type": "application/x-www-form-urlencoded", "body": "q=1"}
-X changes only the method word in the request line. Everything else -d set up, the body and its content type, still went out. Most servers ignore a body on a GET, so this request probably does not do what its author meant. -G is the option that turns -d data into a query string on a GET instead.
POST and redirects
A server endpoint answered with each common redirect code and pointed to /echo. The same curl -s -L -d 'x=1' request followed each one:
| Redirect | Method that arrived | Body |
|---|---|---|
| 301 | GET | dropped |
| 302 | GET | dropped |
| 303 | GET | dropped |
| 307 | POST | x=1 |
| 308 | POST | x=1 |
The manual says that when following a 301, 302 or 303, curl sends the next request as a GET, and that --post301, --post302 and --post303 tell it to keep the POST. With --post302, the 302 case arrived as a POST with its body intact.
The -X POST surprise
Forcing the method with -X POST produced the worst of both worlds after a 302:
$ curl -s -L -X POST -d 'x=1' http://127.0.0.1:8765/redirect/302
{"method": "POST", "path": "/echo", "accept": "*/*", ...}
The method stayed POST because -X overrode curl’s choice, as the manual notes, but the body and its content type were gone. The redirected request was a POST with nothing in it. -X is rarely needed with -d or --json, which already choose POST, and leaving it out avoids this trap.
Errors that look like success
HTTP errors exit with 0 by default
$ curl -s http://127.0.0.1:8765/missing
{"error":"not found"}
$ echo $?
0
The server returned 404, curl printed the error page, and the exit code said success. For curl, a 404 is a successful HTTP conversation. A script that saves this output or pipes it into jq carries on with an error message as if it were data.
-f, and checking the code directly
$ curl -sS -f http://127.0.0.1:8765/missing
curl: (22) The requested URL returned error: 404
$ echo $?
22
The manual describes -f as suppressing output on server errors and returning error 22. The catch is that the error body disappears too, and API error bodies often explain exactly what went wrong. --fail-with-body keeps both: in the test it printed the curl: (22) message, then the server’s {"error":"not found"}, and still exited with 22. For scripts that log failures, that combination is usually the better choice. -S brings back error messages that -s would hide, so -sSf is a useful default for scripts. When the exact status matters, -w prints it:
$ curl -s -o /dev/null -w '%{http_code}\n' http://127.0.0.1:8765/missing
404

Uploads, timeouts and retries
Uploading a file as a form
$ curl -s -F 'file=@data.csv' -F 'note=monthly' http://127.0.0.1:8765/upload
The echo showed a POST with the content type multipart/form-data and a generated boundary string. The body held one part per -F, the file sent under its own name, data.csv, with its contents intact, and the plain text field after it. That is the format a browser uses for a file-upload form, and it is what most “upload” endpoints expect. -d @file is a different thing: it sends the file’s contents as the whole body, not as a named form field.
Never wait forever
A route to an unused address was set to silently drop packets, the way a firewall or a dead server often behaves. With --connect-timeout 3, curl gave up after exactly 3 seconds and exited with code 28, the timeout code. With -m 5 (--max-time), the whole transfer was capped at 5 seconds, also ending with 28. Without either option, a script can sit waiting on a dead server for a long time. Connect timeouts catch unreachable hosts; --max-time also catches servers that accept the connection and then stall.
Retrying
$ curl -sS --retry 3 --retry-delay 1 --retry-connrefused http://127.0.0.1:9/
curl: (7) Failed to connect to 127.0.0.1 port 9 after 0 ms: Couldn't connect to server
Nothing was listening on port 9, so every attempt failed instantly, but the command still took 3 seconds: three retries, one second apart, before exiting with code 7. Without --retry-connrefused, a refused connection is not treated as retryable, and curl gives up on the first try. Retries suit brief outages, such as a service restarting during a deploy, and should be paired with a timeout so a hung server cannot multiply the wait.
A short reference
Copy-ready patterns
# JSON API call with a token, failing on HTTP errors
curl -sSf --json '{"name":"Ana"}' -H "Authorization: Bearer $TOKEN" https://api.example.com/orders
# Send a JSON file
curl -sSf --json @order.json https://api.example.com/orders
# Form values with special characters
curl -sSf --data-urlencode 'q=fish & chips' https://example.com/search
# Query string on a GET
curl -sSf -G --data-urlencode 'q=fish & chips' https://example.com/search
# Just the status code
curl -s -o /dev/null -w '%{http_code}\n' https://example.com/A server that echoes back what it received settles most curl arguments in seconds. The JSON body labeled as a form, the empty POST after a redirect and the 404 that exited with 0 all looked fine from the command line. They only looked wrong from the other end of the wire.
Sources
- curl(1), Ubuntu 24.04 manual page: https://manpages.ubuntu.com/manpages/noble/man1/curl.1.html
