Skip to content
Log In Sign Up info@muvcloud.com

The Basic POST

curl -d "name=deploy&role=admin" https://example.com/api/users

-d switches the request to POST and sends the data as the body, with the content type application/x-www-form-urlencoded, the format an HTML form uses. You do not need -X POST: with -d it is already a POST. Several -d options are joined with an ampersand:

curl -d "name=deploy" -d "role=admin" https://example.com/api/users

Encode Special Characters

curl --data-urlencode "comment=Disk at 90% & rising" https://example.com/api/notes

-d sends the text exactly as written, so an ampersand, a percent sign, or a space inside a value breaks the form. --data-urlencode encodes the part after the equals sign for you.

Send JSON

curl -H "Content-Type: application/json" \
  -d '{"name": "deploy", "role": "admin"}' \
  https://example.com/api/users

Use single quotes around JSON so the shell leaves the double quotes alone. Without the header, curl labels the body as form data, and many APIs reject it. curl 7.82 and later have a shortcut that sets the body and the JSON headers in one option:

curl --json '{"name": "deploy", "role": "admin"}' https://example.com/api/users

Ubuntu 24.04 and 26.04, Debian 12 and 13, and AlmaLinux and Rocky Linux 10 ship a curl new enough for --json. AlmaLinux and Rocky Linux 9 ship an older curl; use the header form there. curl --version shows what you have.

Send Data from a File

curl -H "Content-Type: application/json" --data-binary @./data.json https://example.com/api/users
curl --json @./data.json https://example.com/api/users
cat ./data.json | curl --json @- https://example.com/api/users

An at-sign followed by a path makes curl read the body from that file, and @- reads it from standard input. Prefer --data-binary over -d for files: -d strips line breaks from the file before sending, while --data-binary sends it byte for byte.

Upload a File with -F

curl -F "file=@/home/deploy/report.pdf" https://example.com/upload
curl -F "file=@/home/deploy/report.pdf;type=application/pdf" -F "note=monthly" https://example.com/upload

-F sends multipart/form-data, like an HTML form with a file field. The field name, here file, must match what the server expects. If a large upload fails with status 413, the server's size limit is the cause; see 413 Request Entity Too Large.

Authentication with a Token

A token typed on the command line ends up in your shell history and is visible in the process list while curl runs; see the history command guide. curl can read headers from a file instead. Put the header line in a file only you can read:

Authorization: Bearer API_TOKEN
chmod 600 /home/deploy/.api-headers
curl -H @/home/deploy/.api-headers --json '{"status": "active"}' https://example.com/api/items

For HTTP basic authentication, -u deploy without a password makes curl prompt for it.

See the Response

curl -i --json '{"name": "deploy"}' https://example.com/api/users
curl -v --json '{"name": "deploy"}' https://example.com/api/users
curl -s -o /dev/null -w "%{http_code}\n" -d "name=deploy" https://example.com/api/users
curl --fail-with-body --json @./data.json https://example.com/api/users

-i adds the response headers, -v shows the request as well, and -w prints only the status code. --fail-with-body makes curl exit with an error on a 4xx or 5xx answer while still printing the body, which is what a script needs. The output of -v includes your request headers, so remove the authorization line before you share it. The status codes are listed in HTTP status codes explained.

Other Methods and Common Problems

curl -X PUT --json '{"role": "editor"}' https://example.com/api/users/42
curl -X DELETE https://example.com/api/users/42
  • 415 Unsupported Media Type: the Content-Type header is missing or wrong.
  • 400 Bad Request with JSON: the quoting broke the JSON. Put it in a file and check it with jq . data.json.
  • 405 Method Not Allowed: the address does not accept POST; see 405 Method Not Allowed.
  • A POST that turns into a GET: with -L, curl follows a 301, 302, or 303 redirect with a GET. Post to the final address, often the HTTPS one or the one with a trailing slash.

For downloads rather than requests, see wget vs curl.

Updated:

Linux VPS with Full Root Access

MUVCloud cloud servers run on KVM with Enterprise NVMe SSD storage and full root access, in Turkey, the Netherlands, and the USA.

Cloud VPS Plans Starting at2.99 $/mo Cheap Linux VPS

No Limits on Service or Support

You are not on your own as your projects grow. Our support engineers are ready 24/7, on several channels, whenever you need them.

Support specialist
  • 100% customer satisfaction goal
  • Our own hardware
  • 24/7 ticket and email support
  • 4 countries with locations