Send an HTTP POST request
POST a JSON body (or form fields, or a file) to a URL and capture the response.
How to send an http post request in each shell
curl -sS -X POST -H "Content-Type: application/json" -d '{"name":"alice"}' https://api.example.com/userscurl -sS -X POST -H "Content-Type: application/json" -d '{"name":"alice"}' https://api.example.com/usersIdentical to bash. Zsh's `=` history-expansion bites if your URL contains `!` — quote it.
curl -sS -X POST -H "Content-Type: application/json" -d '{"name":"alice"}' https://api.example.com/usersFish has no built-in HTTP client. Quote `$variables` with `"$var"` because fish does NOT word-split by default — backslash-newline continuations need `\` not bash's `\\`.
Invoke-RestMethod -Method Post -Uri https://api.example.com/users -ContentType application/json -Body '{"name":"alice"}'`Invoke-RestMethod` AUTO-PARSES JSON responses into objects (use `Invoke-WebRequest` if you want the raw `HttpResponseMessage` with `.Content`/`.StatusCode`/`.Headers`). PowerShell 5.1 defaults to TLS 1.0 — `[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12` once per session if hitting modern APIs.
curl -sS -X POST -H "Content-Type: application/json" -d "{\"name\":\"alice\"}" https://api.example.com/userscmd ships `curl.exe` in `C:\Windows\System32\` since Windows 10 1803. JSON quoting is the pain — outer `"…"` + inner `\"…\"`. For complex bodies write the JSON to a file and use `--data @body.json`.
Equivalents listed for Bash, Zsh, Fish, PowerShell, cmd.exe.
Gotchas & notes
- `-d` is `--data`, which **defaults to** `Content-Type: application/x-www-form-urlencoded` if you don't set `-H "Content-Type: application/json"` explicitly. The #1 newcomer trap: posting `'{"a":1}'` without the header makes the server reject it as malformed form-data. Always pair `-d` JSON with `-H "Content-Type: application/json"`.
- Form fields (`application/x-www-form-urlencoded`): `curl -sS -X POST -d "name=alice&age=30" URL`. Multipart (`multipart/form-data`, e.g. file upload): `curl -sS -X POST -F "[email protected]" -F "caption=hello" URL`. `-F` vs `-d` selects encoding; mixing produces invalid request bodies.
- PowerShell `Invoke-RestMethod -Body $hashtable` auto-encodes a `[hashtable]` as `application/x-www-form-urlencoded`. To send JSON pass a `[string]` (`-Body ($obj | ConvertTo-Json -Depth 100)`) and set `-ContentType application/json` explicitly. `-Depth 100` is mandatory on pwsh 5.1 where the default truncates nested objects to `"System.Collections.Hashtable"` strings.
- Authentication: `curl -sS -u user:pass URL` (Basic), `curl -sS -H "Authorization: Bearer $TOKEN" URL` (Bearer). PowerShell: `-Credential (Get-Credential)` (Basic prompt) or `-Headers @{Authorization="Bearer $TOKEN"}` (Bearer). NEVER hardcode tokens — load from env vars (`$env:API_TOKEN`) or use a secrets manager; tokens in shell history are routinely leaked.
Related commands
Related tasks
- Get HTTP response headers— Inspect just the response headers of a URL — useful for debugging redirects, caching, CORS, and TLS.
- Check if a URL is reachable— Test whether a URL returns 2xx/3xx — useful for healthchecks, wait-for-it scripts, and CI smoke tests.
- Follow HTTP redirects— Print the redirect chain (301/302/303/307/308 hops) from a starting URL to its final 2xx destination.
- Download a file from a URL— Save a remote file to disk via HTTP/HTTPS, with progress and resume where supported.