Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
REST API calls with Invoke-RestMethod
📖 Scenario: You are working with a public REST API that provides information about users. You want to fetch data from this API and process it in PowerShell.
🎯 Goal: Build a PowerShell script that calls a REST API using Invoke-RestMethod, filters the results based on a condition, and displays the filtered data.
📋 What You'll Learn
Create a variable with the API URL
Create a variable for the minimum user ID to filter
Use Invoke-RestMethod to get data from the API
Filter users with IDs greater than or equal to the minimum ID
Print the filtered users' names and IDs
💡 Why This Matters
🌍 Real World
Calling REST APIs is common when working with web services to get or send data automatically.
💼 Career
Many IT and automation jobs require scripting to interact with APIs for monitoring, reporting, or integrating systems.
Progress0 / 4 steps
1
Set the API URL
Create a variable called apiUrl and set it to the string "https://jsonplaceholder.typicode.com/users".
PowerShell
Hint
Use $apiUrl = "https://jsonplaceholder.typicode.com/users" to store the URL.
2
Set the minimum user ID filter
Create a variable called minUserId and set it to the number 5.
PowerShell
Hint
Use $minUserId = 5 to set the filter value.
3
Fetch and filter users
Use Invoke-RestMethod with $apiUrl to get the users and store them in a variable called users. Then create a variable called filteredUsers that contains only users with id greater than or equal to $minUserId.
PowerShell
Hint
Use $users = Invoke-RestMethod -Uri $apiUrl to get data. Use Where-Object { $_.id -ge $minUserId } to filter.
4
Display filtered users
Use a foreach loop to print each user's id and name from $filteredUsers in the format: User ID: [id], Name: [name].
PowerShell
Hint
Use foreach ($user in $filteredUsers) { Write-Output "User ID: $($user.id), Name: $($user.name)" } to print.
Practice
(1/5)
1. What does the PowerShell cmdlet Invoke-RestMethod primarily do?
easy
A. It sends HTTP requests to web APIs and returns parsed responses.
B. It creates new files on the local system.
C. It compiles PowerShell scripts into executables.
D. It manages Windows services remotely.
Solution
Step 1: Understand the purpose of Invoke-RestMethod
This cmdlet is designed to send HTTP requests to REST APIs and automatically parse the response into PowerShell objects.
Step 2: Compare options with the cmdlet's function
Options B, C, and D describe unrelated tasks like file creation, compiling, or service management, which Invoke-RestMethod does not perform.
Final Answer:
It sends HTTP requests to web APIs and returns parsed responses. -> Option A
Quick Check:
Invoke-RestMethod = Sends HTTP requests [OK]
Hint: Invoke-RestMethod calls APIs and returns objects [OK]
Common Mistakes:
Confusing Invoke-RestMethod with file or service commands
Thinking it only sends GET requests
Assuming it returns raw text instead of parsed objects
2. Which of the following is the correct syntax to send a GET request to https://api.example.com/data using Invoke-RestMethod?
easy
A. Invoke-RestMethod -Url https://api.example.com/data -Method GET
B. Invoke-RestMethod -Url https://api.example.com/data -Method POST
C. Invoke-RestMethod -Uri https://api.example.com/data -Method FETCH
D. Invoke-RestMethod -Uri https://api.example.com/data -Method GET
Solution
Step 1: Identify correct parameter names and HTTP method
The correct parameter for the URL is '-Uri' and the HTTP method for retrieving data is 'GET'.
Step 2: Check each option for syntax correctness
Invoke-RestMethod -Uri https://api.example.com/data -Method GET uses '-Uri' and '-Method GET' correctly. Invoke-RestMethod -Url https://api.example.com/data -Method POST uses '-Url' (incorrect parameter) and POST method. Invoke-RestMethod -Url https://api.example.com/data -Method GET uses '-Url' (incorrect parameter). Invoke-RestMethod -Uri https://api.example.com/data -Method FETCH uses an invalid HTTP method 'FETCH'.
Final Answer:
Invoke-RestMethod -Uri https://api.example.com/data -Method GET -> Option D
Quick Check:
Use -Uri and -Method GET for GET requests [OK]
Hint: Use -Uri for URL and -Method GET for GET requests [OK]
Common Mistakes:
Using -Url instead of -Uri
Using -Method POST instead of GET
Using invalid HTTP methods like FETCH
3. What will be the output of this PowerShell code snippet?
$response = Invoke-RestMethod -Uri 'https://jsonplaceholder.typicode.com/posts/1' -Method GET $response.title
medium
A. Null, because $response.title does not exist
B. An error because the -Method parameter is missing
C. The title of the post with ID 1 from the API
D. The entire JSON response as a string
Solution
Step 1: Understand Invoke-RestMethod behavior
Invoke-RestMethod sends a GET request and parses JSON into an object. Accessing $response.title retrieves the 'title' property.
Step 2: Analyze the code and expected output
The API returns a JSON object with a 'title' field for post ID 1. The code prints that title string.
Final Answer:
The title of the post with ID 1 from the API -> Option C
Hint: Invoke-RestMethod returns objects; access properties like .title [OK]
Common Mistakes:
Expecting raw JSON string instead of parsed object
Forgetting to specify -Method GET (optional but recommended)
Assuming $response.title is null without checking API
4. You run this command but get an error:
Invoke-RestMethod -Uri 'https://api.example.com/data' -Method POST -Body '{"name":"John"}'
What is the most likely cause?
medium
A. The -Method POST is invalid; only GET is allowed.
B. The -Body parameter must be a PowerShell object, not a JSON string.
C. The URI is missing the protocol (http/https).
D. Invoke-RestMethod cannot send POST requests.
Solution
Step 1: Understand -Body parameter requirements
Invoke-RestMethod expects the -Body parameter as a PowerShell object or properly formatted string with correct headers.
Step 2: Identify issue with JSON string body
Passing a raw JSON string without setting Content-Type header or converting to object causes errors.
Final Answer:
The -Body parameter must be a PowerShell object, not a JSON string. -> Option B
Quick Check:
Use objects or set headers when sending JSON body [OK]
Hint: Send objects or set headers when posting JSON body [OK]
Common Mistakes:
Passing JSON string without Content-Type header
Assuming POST is unsupported
Omitting protocol in URI
5. You want to send a POST request with JSON data {"username":"admin","password":"pass123"} to https://api.example.com/login using Invoke-RestMethod. Which script correctly sends the request and handles the JSON response?
hard
A. $body = @{username='admin'; password='pass123'} Invoke-RestMethod -Uri 'https://api.example.com/login' -Method POST -Body ($body | ConvertTo-Json) -ContentType 'application/json'
B. $body = '{"username":"admin","password":"pass123"}' Invoke-RestMethod -Uri 'https://api.example.com/login' -Method POST -Body $body
C. $body = @{username='admin'; password='pass123'} Invoke-RestMethod -Uri 'https://api.example.com/login' -Method GET -Body $body
D. Invoke-RestMethod -Uri 'https://api.example.com/login' -Method POST -Body @{username='admin'; password='pass123'}
Solution
Step 1: Prepare the body as a PowerShell object and convert to JSON
Creating a hashtable and converting it to JSON ensures the body is correctly formatted for the API.
Step 2: Use -ContentType 'application/json' to inform the server
Setting the Content-Type header is necessary for the server to interpret the JSON body correctly.
Step 3: Verify the HTTP method is POST
POST is required to send data; GET with body is invalid.
Final Answer:
$body = @{username='admin'; password='pass123'} Invoke-RestMethod -Uri 'https://api.example.com/login' -Method POST -Body ($body | ConvertTo-Json) -ContentType 'application/json' -> Option A
Quick Check:
Convert body to JSON and set Content-Type for POST [OK]
Hint: Convert body to JSON and set Content-Type for POST [OK]
Common Mistakes:
Sending raw JSON string without Content-Type header
Using GET method with body
Passing hashtable directly without JSON conversion