Bird
0
0

Given this code snippet:

medium📝 Command Output Q5 of 15
PowerShell - Cross-Platform PowerShell
Given this code snippet:
$response = Invoke-RestMethod -Uri 'https://api.example.com/data' -Method POST -Body @{id=5; name='Alice'} -ContentType 'application/json'
$response.status

What will be the output if the API returns {"status":"success"}?
A5
B@{id=5; name=Alice}
Csuccess
DError: Cannot convert body to JSON
Step-by-Step Solution
Solution:
  1. Step 1: Understand body conversion

    The hashtable in -Body is automatically converted to JSON because -ContentType 'application/json' is specified.
  2. Step 2: Access response property

    The API returns JSON with a 'status' property; Invoke-RestMethod converts it to an object, so $response.status returns 'success'.
  3. Final Answer:

    success -> Option C
  4. Quick Check:

    Response property access = success [OK]
Quick Trick: Hashtable body auto-converts to JSON with correct ContentType [OK]
Common Mistakes:
  • Expecting raw hashtable output
  • Missing ContentType causes errors
  • Confusing response with request body

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes