Powershell Invoke-RestMethod GET and POST Example with parameters

powershell invoke restmethodPOST Examplepowershell Invoke RestMethod

Today in this article, we shall see how to use the PowerShell invoke-restmethod utility method to perform API GET and POST requests with parameters.

We shall cover below in today’s article,

The Invoke-RestMethod cmdlet sends HTTP and HTTPS and also supports REST( Representational State transfer) requests to a WEB API or service. It efficiently deals with JSON and XML content.

Powershell Invoke-RestMethod GET request

Below is the Invoke-RestMethod GET request example.

Example

PS C:\Test\File\load-sqldata>  Invoke-RestMethod https://catfact.ninja/fact

fact                                                                                                                                                                     
----                                                                                                                                                                     
Researchers are unsure exactly how a cat purrs. Most veterinarians believe that a cat purrs by vibrating vocal folds deep in the throat. To do this, a muscle in the l..

Invoke RestMethod POST with JWT bearer invoke restmethod

Powershell Invoke-RestMethod POST request

Let’s look at the Invoke-RestMethod POST request example below.

Below is an equivalent body parameter example in PowerShell,

$body = @{
    "siteUrl" ="https://www.thecodebuzz.com"
    "email" = "[email protected]"
}

Below is the POST method example using Invoke-RestMethod,

Example

Invoke-RestMethod -Method 'Post' -Uri $url -Body ($body|ConvertTo-Json) -Headers $headers -ContentType "application/json"

Powershell Invoke-RestMethod POST request with Headers

Let’s look at the Invoke-RestMethod POST request with headers example.

Powershell requests with headers can be defined as below,

$headers = @{
 'Content-Type'='application/json'
 'apikey'='475646456456'
 }

Below is the Invoke-RestMethod POST method example with Headers,

Example

Invoke-RestMethod -Method 'Post' -Uri $url -Body ($body|ConvertTo-Json) -Headers $headers -ContentType "application/json"

Powershell Post request with JSON array example

Example here – Powershell Post request with JSON array example

Powershell Invoke-RestMethod POST request with Authentication headers

Let’s look at the Invoke-RestMethod POST request with Authentication headers.

APIs require you to authenticate via different authentication schemes like,

  • Basic authentication
  • OAuth token
  • JWT Bearer Authentication

You can call Invoke-RestMethod POST request with Authentication headers like JWT bearer token as below,

JWT Bearer Authentication using Powershell

JSON Web Token( JWT) is an open standard used for securely transmitting information between parties as a JSON object. 

The client should always send the Authorization header with Bearer schema as below.

Authorization: Bearer <token>

Example :

Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.
eyJleHAiOjE1NzEwMDc3MzQsImlzcyI6Imh0dHBzOi8vbG9jYWxob3N0OjQ0MzQxIiwiYXVkIjoiaHR0cHM6Ly9sb2NhbGhvc3Q6NDQzNDEifQ.Ve2x6ueiHPyrP7wLjpmZrhx7QWrQKnkxWsu_1qUKKLA

Below is how the header can be defined to send with any request.

$token"='xxxxxxxxxxx'
$headers = @{
 'Content-Type'='application/json'
 'Authorization'= 'Bearer $token'
 }

Example

Invoke-RestMethod -Method 'Post' -Uri $url -Body ($body|ConvertTo-Json) -Headers $headers

Basic Authentication with PowerShell

Basic authentication is an Authentication Scheme built into the HTTP protocol which uses a simple username and password to access a restricted resource.

If you have UserName and Password is as “Test“, “Password” then Base64 string token should be as below,

AuthorizationBasic VGVzdDpQYXNzd29yZA===

Create base64 string using Powershell in detail in the below article,

$token"='xxxxxxxxxxx' 
$headers = @{
 'Content-Type'='application/json'
 'Authorization'= 'Basic $token'
 }

Once you add the required secured header, you simply call API using the below way,

Example

Invoke-RestMethod -Method 'Post' -Uri $url -Body ($body|ConvertTo-Json) -Headers $headers

References :

Do you have any comments or ideas or any better suggestions to share?

Please sound off your comments below.

Happy Coding !!



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



Leave a Reply

Your email address will not be published. Required fields are marked *