Bird
Raised Fist0
PowerShellscripting~5 mins

REST API calls with Invoke-RestMethod in PowerShell - Cheat Sheet & Quick Revision

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
Recall & Review
beginner
What is the purpose of the <code>Invoke-RestMethod</code> cmdlet in PowerShell?
It is used to send HTTP and HTTPS requests to REST APIs and receive responses, making it easy to interact with web services.
Click to reveal answer
beginner
How do you specify the HTTP method (GET, POST, etc.) when using Invoke-RestMethod?
Use the -Method parameter with values like 'GET', 'POST', 'PUT', or 'DELETE' to specify the HTTP method.
Click to reveal answer
intermediate
How can you send JSON data in a POST request using Invoke-RestMethod?
Convert your data to JSON using ConvertTo-Json and pass it with the -Body parameter. Also, set the -ContentType to 'application/json'.
Click to reveal answer
beginner
What does Invoke-RestMethod return when calling a REST API?
It returns the response content parsed into PowerShell objects, such as hashtables or arrays, making it easy to work with the data.
Click to reveal answer
intermediate
How do you add headers like API keys to your REST API call with Invoke-RestMethod?
Use the -Headers parameter with a hashtable containing header names and values, for example: @{'Authorization'='Bearer token'}.
Click to reveal answer
Which parameter do you use to specify the HTTP method in Invoke-RestMethod?
A-Uri
B-Method
C-Body
D-Headers
How do you send JSON data in a POST request using Invoke-RestMethod?
AUse <code>-Body</code> with a JSON string and set <code>-ContentType</code> to 'application/json'
BUse <code>-Headers</code> only
CUse <code>-Uri</code> with JSON data
DUse <code>-Method</code> with 'JSON'
What does Invoke-RestMethod return after a successful API call?
AParsed PowerShell objects
BNothing
CRaw text only
DOnly HTTP status code
How do you add an API key in the headers for a REST call?
AUse <code>-Method</code> with the API key
BUse <code>-Body</code> with the API key
CUse <code>-Headers</code> with a hashtable containing the API key
DUse <code>-Uri</code> with the API key
Which HTTP method is the default for Invoke-RestMethod if not specified?
APOST
BPUT
CDELETE
DGET
Explain how to perform a POST request with JSON data using Invoke-RestMethod in PowerShell.
Think about how to tell the API you are sending data and how to format it.
You got /4 concepts.
    Describe how to include custom headers, such as an API key, in a REST API call using Invoke-RestMethod.
    Headers are like extra information you send with your request.
    You got /3 concepts.

      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