Code Reference

REST Get JSON

Powershell · Example / how-to

REST Get JSON

Powershell · Example / how-to


📋 Overview

Call a JSON REST API with Invoke-RestMethod, handle errors, and select fields from the response.

🔧 Core concepts

PieceRole
Invoke-RestMethodParse JSON automatically
HeadersAccept / auth
try/catchSurface HTTP errors
Pipeline selectShape output

💡 Examples

rest_get_json.ps1:

param(
    [string]$UserId = "1"
)

$ErrorActionPreference = "Stop"
$url = "https://jsonplaceholder.typicode.com/users/$UserId"

try {
    $user = Invoke-RestMethod -Uri $url -Method Get -Headers @{
        Accept = "application/json"
    }
} catch {
    Write-Error "Request failed: $($_.Exception.Message)"
    exit 1
}

[pscustomobject]@{
    Id    = $user.id
    Name  = $user.name
    Email = $user.email
    City  = $user.address.city
} | Format-List

With token:

$token = $env:API_TOKEN
Invoke-RestMethod -Uri "https://api.example.com/v1/items" -Headers @{
    Authorization = "Bearer $token"
    Accept        = "application/json"
}

⚠️ Pitfalls

  • Secrets in scripts should come from env vars / secret stores, not literals.
  • Invoke-WebRequest returns raw response; prefer Invoke-RestMethod for JSON.
  • TLS / proxy issues often need [Net.ServicePointManager] only as a last resort.

On this page