Skip to content
shellmap

invoke-restmethodHTTP cmdlet that auto-parses JSON — curl plus jq combined across all 5 shells

Equivalents in every shell

Bashunix
curl -s https://api.example.com/users/1 | jq

`curl -s` (silent) drops the progress meter so the JSON pipes cleanly into `jq`. Use `jq -r .field` for raw string output (no surrounding quotes) when feeding the value into another shell command.

Zshunix
curl -s https://api.example.com/users/1 | jq

Same external `curl` and `jq`. Both ship on macOS via Homebrew (`brew install jq`) — base macOS has curl but not jq. Linux distros usually ship both via `apt`/`dnf`/`pacman`.

Fishunix
curl -s https://api.example.com/users/1 | jq

Same external pipeline. Fish's cleaner quoting helps with URLs that contain shell-special characters — `curl -s 'https://api.example.com/search?q=foo&type=bar'` works without backslash-escaping the `&`.

PowerShellwindows
Invoke-RestMethod https://api.example.com/users/1

Auto-detects `Content-Type: application/json` and returns the parsed object graph as `[PSCustomObject]` — `.id`, `.name` etc. all work as property accessors without an explicit `ConvertFrom-Json` step. Aliased as `irm`. For form-encoded or XML responses, use `Invoke-WebRequest` instead.

cmd.exewindows
curl -s https://api.example.com/users/1

Windows 10 1803+ ships `curl.exe` natively in `C:\Windows\System32`. cmd.exe itself has no JSON parser — pipe to `powershell -c "$input | ConvertFrom-Json"` or install `jq` via `winget install jqlang.jq`.

Worked examples

POST a JSON body with a bearer token

Bash
curl -s -X POST -H 'Authorization: Bearer $TOKEN' -H 'Content-Type: application/json' -d '{"name":"alice"}' https://api.example.com/users
PowerShell
Invoke-RestMethod -Method POST -Uri https://api.example.com/users -Headers @{Authorization="Bearer $env:TOKEN"} -Body (@{name='alice'} | ConvertTo-Json) -ContentType 'application/json'
cmd.exe
curl -s -X POST -H "Authorization: Bearer %TOKEN%" -H "Content-Type: application/json" -d "{\"name\":\"alice\"}" https://api.example.com/users

Download a paginated list and extract one field per page

Bash
for p in 1 2 3; do curl -s "https://api.example.com/items?page=$p" | jq -r '.data[].id'; done
PowerShell
1..3 | ForEach-Object { (Invoke-RestMethod "https://api.example.com/items?page=$_").data.id }

Capture response headers alongside the parsed body

Bash
curl -s -D /tmp/headers.txt https://api.example.com/users/1 | jq && cat /tmp/headers.txt
PowerShell
Invoke-RestMethod https://api.example.com/users/1 -ResponseHeadersVariable hdrs; $hdrs

Gotchas

  • `Invoke-RestMethod` only auto-parses responses whose `Content-Type` header starts with `application/json` (or `text/xml`/`application/xml` → XmlDocument). Some APIs send `Content-Type: application/octet-stream` or `text/plain` for JSON — in that case the cmdlet returns the raw string, and you must call `ConvertFrom-Json` yourself. Inspect with `(Invoke-WebRequest …).Headers.'Content-Type'` if the auto-deserialise mysteriously yields a string.
  • `-SkipCertificateCheck` is PowerShell 7+ only. On Windows PowerShell 5.1, to disable TLS validation (testing only) you must hack the PROCESS-WIDE `ServicePointManager` callback: `[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }`. Never ship this in a production script — every HTTPS call in the session becomes unvalidated.
  • On 5.1, `Invoke-RestMethod` uses the legacy `mshtml.dll` (Internet Explorer) HTML parser for non-JSON responses — slow, and outright fails if IE is uninstalled. Pass `-UseBasicParsing` on 5.1; on pwsh 7+ the flag is a no-op (the modern parser is the only path) but is still accepted.
  • Default `-MaximumRedirection 5` differs from curl's default of NOT following redirects (curl needs `-L` to follow). Scripts ported from curl that assume no-follow can produce wrong-URL results when the API starts returning 301s. To match curl's default, set `-MaximumRedirection 0`.
  • The entire response body buffers in memory before the cmdlet returns — for multi-GB downloads use `Invoke-WebRequest -OutFile` (streams to disk) or shell out to `curl.exe`. There is no built-in chunked-streaming API; `-ResponseHeadersVariable` exposes headers early but the body is still fully buffered.

WSL & PowerShell Core notes

pwshpwsh 7+ ships the modernised parser, `-SkipCertificateCheck`, `-Authentication Basic|Bearer|OAuth`, and `-AllowUnencryptedAuthentication`. On Linux/macOS pwsh, the `curl` alias is REMOVED so the system `/usr/bin/curl` binary resolves first — `Invoke-RestMethod` and `irm` remain. On Windows pwsh 5.1, `curl` is an alias for `Invoke-WebRequest`, which surprises ported scripts (`-X POST -d` will error).
WSLFrom WSL bash, `pwsh.exe -c "Invoke-RestMethod https://..."` reaches Windows networking (Windows certificate store, proxy settings, etc.) — useful when WSL's Linux curl gets routed differently. For pure-Linux behaviour stay in bash; for Windows-cert-trust behaviour shell out to pwsh.

Common tasks using invoke-restmethod

Related commands