0
0
PowerShellscripting~20 mins

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

Choose your learning style9 modes available
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.