Bird
Raised Fist0
PowerShellscripting~20 mins

REST API calls with Invoke-RestMethod in PowerShell - Practice Problems & Coding Challenges

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
Challenge - 5 Problems
🎖️
REST API PowerShell Pro
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this Invoke-RestMethod GET request?
Given the following PowerShell code that calls a public API, what will be the output?
PowerShell
Invoke-RestMethod -Uri 'https://api.agify.io?name=michael' | Select-Object -Property name,age
AInvoke-RestMethod : The remote server returned an error: (404) Not Found.
B@{name=undefined; age=69}
C@{name=michael; age=undefined}
D@{name=michael; age=69}
Attempts:
2 left
💡 Hint
The API returns predicted age for a given name in JSON format, which Invoke-RestMethod converts to a PowerShell object.
📝 Syntax
intermediate
2:00remaining
Which option correctly sends a POST request with JSON body using Invoke-RestMethod?
You want to send a POST request with JSON data '{"username":"user1","password":"pass"}'. Which PowerShell command is correct?
AInvoke-RestMethod -Uri 'https://example.com/api/login' -Method POST -Body '{username:user1,password:pass}' -ContentType 'application/json'
BInvoke-RestMethod -Uri 'https://example.com/api/login' -Method POST -Body @{username='user1'; password='pass'} -ContentType 'application/json'
CInvoke-RestMethod -Uri 'https://example.com/api/login' -Method POST -Body '{"username":"user1","password":"pass"}' -ContentType 'application/json'
DInvoke-RestMethod -Uri 'https://example.com/api/login' -Method POST -Body '{"username":"user1","password":"pass"}'
Attempts:
2 left
💡 Hint
The body must be a JSON string and ContentType must be set to 'application/json'.
🔧 Debug
advanced
2:00remaining
Why does this Invoke-RestMethod call fail with a 401 Unauthorized error?
You run this command to access a protected API but get a 401 error: Invoke-RestMethod -Uri 'https://api.example.com/data' -Headers @{Authorization='Bearer mytoken123'} What is the most likely cause?
AThe token 'mytoken123' is invalid or expired, causing the 401 Unauthorized error.
BThe Authorization header key should be lowercase 'authorization' instead of 'Authorization'.
CInvoke-RestMethod does not support the -Headers parameter.
DThe URI is missing the query parameter 'access_token=mytoken123'.
Attempts:
2 left
💡 Hint
401 Unauthorized means the server rejected the credentials.
🚀 Application
advanced
2:00remaining
How to extract the 'id' field from a JSON response using Invoke-RestMethod?
You run this command: $response = Invoke-RestMethod -Uri 'https://jsonplaceholder.typicode.com/posts/1' How do you get the 'id' value from $response?
AGet-Content $response.id
B$response.id
C$response->id
D$response['id']()
Attempts:
2 left
💡 Hint
Invoke-RestMethod converts JSON to a PowerShell object with properties.
🧠 Conceptual
expert
2:00remaining
What happens if you omit -ContentType when sending JSON data with Invoke-RestMethod POST?
You run this command: Invoke-RestMethod -Uri 'https://api.example.com/submit' -Method POST -Body '{"key":"value"}' What is the most likely result?
AThe server may reject the request or treat the body as plain text, causing unexpected behavior.
BInvoke-RestMethod automatically detects JSON and sets ContentType to 'application/json'.
CThe command will fail with a syntax error due to missing ContentType.
DThe server will accept the request and parse the JSON correctly without ContentType.
Attempts:
2 left
💡 Hint
ContentType informs the server how to interpret the body data.

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