REST API calls with Invoke-RestMethod in PowerShell - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When using Invoke-RestMethod to call REST APIs, it's important to understand how the time taken grows as you make more calls.
We want to know how the total time changes when the number of API requests increases.
Analyze the time complexity of the following code snippet.
$urls = @('https://api.example.com/data1', 'https://api.example.com/data2', 'https://api.example.com/data3')
foreach ($url in $urls) {
$response = Invoke-RestMethod -Uri $url -Method Get
Write-Output $response
}
This code sends a GET request to each URL in the list and outputs the response.
- Primary operation: The foreach loop that sends an API request for each URL.
- How many times: Once for each URL in the list.
Each additional URL means one more API call, so the total time grows as you add more URLs.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 API calls |
| 100 | 100 API calls |
| 1000 | 1000 API calls |
Pattern observation: The total work grows directly with the number of URLs.
Time Complexity: O(n)
This means the time to complete all API calls grows linearly with the number of URLs.
[X] Wrong: "All API calls happen instantly or at the same time, so adding more URLs doesn't increase time much."
[OK] Correct: Each API call waits for a response before moving on, so more calls add more total time.
Understanding how repeated API calls affect time helps you write scripts that scale well and manage waiting times effectively.
"What if we changed the code to send all API requests at the same time using background jobs? How would the time complexity change?"
Practice
Invoke-RestMethod primarily do?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 AQuick Check:
Invoke-RestMethod = Sends HTTP requests [OK]
- Confusing Invoke-RestMethod with file or service commands
- Thinking it only sends GET requests
- Assuming it returns raw text instead of parsed objects
https://api.example.com/data using Invoke-RestMethod?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 DQuick Check:
Use -Uri and -Method GET for GET requests [OK]
- Using -Url instead of -Uri
- Using -Method POST instead of GET
- Using invalid HTTP methods like FETCH
$response = Invoke-RestMethod -Uri 'https://jsonplaceholder.typicode.com/posts/1' -Method GET
$response.title
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 CQuick Check:
Invoke-RestMethod parses JSON; access properties directly [OK]
- Expecting raw JSON string instead of parsed object
- Forgetting to specify -Method GET (optional but recommended)
- Assuming $response.title is null without checking API
Invoke-RestMethod -Uri 'https://api.example.com/data' -Method POST -Body '{"name":"John"}'What is the most likely cause?
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 BQuick Check:
Use objects or set headers when sending JSON body [OK]
- Passing JSON string without Content-Type header
- Assuming POST is unsupported
- Omitting protocol in URI
{"username":"admin","password":"pass123"} to https://api.example.com/login using Invoke-RestMethod. Which script correctly sends the request and handles the JSON response?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 AQuick Check:
Convert body to JSON and set Content-Type for POST [OK]
- Sending raw JSON string without Content-Type header
- Using GET method with body
- Passing hashtable directly without JSON conversion
