Bird
Raised Fist0
PowerShellscripting~5 mins

REST API calls with Invoke-RestMethod in PowerShell - Time & Space Complexity

Choose your learning style10 modes available

Start learning this pattern below

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
Time Complexity: REST API calls with Invoke-RestMethod
O(n)
Understanding Time Complexity

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.

Scenario Under Consideration

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.

Identify Repeating Operations
  • Primary operation: The foreach loop that sends an API request for each URL.
  • How many times: Once for each URL in the list.
How Execution Grows With Input

Each additional URL means one more API call, so the total time grows as you add more URLs.

Input Size (n)Approx. Operations
1010 API calls
100100 API calls
10001000 API calls

Pattern observation: The total work grows directly with the number of URLs.

Final Time Complexity

Time Complexity: O(n)

This means the time to complete all API calls grows linearly with the number of URLs.

Common Mistake

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

Interview Connect

Understanding how repeated API calls affect time helps you write scripts that scale well and manage waiting times effectively.

Self-Check

"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

(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

  1. 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.
  2. 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.
  3. Final Answer:

    It sends HTTP requests to web APIs and returns parsed responses. -> Option A
  4. 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

  1. 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'.
  2. 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'.
  3. Final Answer:

    Invoke-RestMethod -Uri https://api.example.com/data -Method GET -> Option D
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    The title of the post with ID 1 from the API -> Option C
  4. Quick Check:

    Invoke-RestMethod parses JSON; access properties directly [OK]
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

  1. Step 1: Understand -Body parameter requirements

    Invoke-RestMethod expects the -Body parameter as a PowerShell object or properly formatted string with correct headers.
  2. Step 2: Identify issue with JSON string body

    Passing a raw JSON string without setting Content-Type header or converting to object causes errors.
  3. Final Answer:

    The -Body parameter must be a PowerShell object, not a JSON string. -> Option B
  4. 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

  1. 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.
  2. 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.
  3. Step 3: Verify the HTTP method is POST

    POST is required to send data; GET with body is invalid.
  4. 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
  5. 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