Challenge - 5 Problems
REST API PowerShell Pro
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2: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,ageAttempts:
2 left
💡 Hint
The API returns predicted age for a given name in JSON format, which Invoke-RestMethod converts to a PowerShell object.
✗ Incorrect
The API 'https://api.agify.io' predicts age for the name 'michael'. The output is a PowerShell object with properties 'name' and 'age'.
📝 Syntax
intermediate2: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?
Attempts:
2 left
💡 Hint
The body must be a JSON string and ContentType must be set to 'application/json'.
✗ Incorrect
Option C correctly sends a JSON string as body with the proper content type. Option C sends a PowerShell hashtable which is not automatically converted to JSON. Option C has invalid JSON syntax. Option C misses ContentType, so server may reject.
🔧 Debug
advanced2: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?
Attempts:
2 left
💡 Hint
401 Unauthorized means the server rejected the credentials.
✗ Incorrect
The header name 'Authorization' is case-insensitive and correct. Invoke-RestMethod supports -Headers. The token is likely invalid or expired. The API expects the token in the Authorization header, not as a query parameter.
🚀 Application
advanced2: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?
Attempts:
2 left
💡 Hint
Invoke-RestMethod converts JSON to a PowerShell object with properties.
✗ Incorrect
You access properties of PowerShell objects with dot notation, so $response.id returns the 'id' value. Option B is invalid syntax. Option B is not PowerShell syntax. Option B tries to use Get-Content on a value, which is incorrect.
🧠 Conceptual
expert2: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?
Attempts:
2 left
💡 Hint
ContentType informs the server how to interpret the body data.
✗ Incorrect
Without specifying -ContentType, the server may treat the body as plain text or default content type, leading to errors or ignored data. Invoke-RestMethod does not auto-set ContentType based on body content. The command runs but server behavior depends on ContentType header.