Skip to content
shellmap

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

Bashunix
curl -sS -X POST -H "Content-Type: application/json" -d '{"name":"alice"}' https://api.example.com/users
Zshunix
curl -sS -X POST -H "Content-Type: application/json" -d '{"name":"alice"}' https://api.example.com/users

Identical to bash. Zsh's `=` history-expansion bites if your URL contains `!` — quote it.

Fishunix
curl -sS -X POST -H "Content-Type: application/json" -d '{"name":"alice"}' https://api.example.com/users

Fish 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 `\\`.

PowerShellwindows
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.

cmd.exewindows
curl -sS -X POST -H "Content-Type: application/json" -d "{\"name\":\"alice\"}" https://api.example.com/users

cmd 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