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
Step 1: Understand -Body parameter requirements
Invoke-RestMethod expects the -Body parameter as a PowerShell object or properly formatted string with correct headers.
Step 2: Identify issue with JSON string body
Passing a raw JSON string without setting Content-Type header or converting to object causes errors.
Final Answer:
The -Body parameter must be a PowerShell object, not a JSON string. -> Option B
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
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.
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.
Step 3: Verify the HTTP method is POST
POST is required to send data; GET with body is invalid.
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
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