Powershell Invoke WebRequest post example

Today in this article, we shall see how to use PowerShell Invoke-WebRequest a utility method to call HTTP GET and POST request with parameters.

The Invoke-WebRequest cmdlet sends HTTP and HTTPS and also supports FTP etc and requests to web pages or web services.

It lets you collect links, images, and other HTML elements as per your requirements and Efficiently deal with HTML content.

Today, we shall cover how to use Invoke-WebRequest for below,

Powershell Invoke-WebRequest GET request

Below is Invoke-WebRequest GET request example. Here we are using the https://catfact.ninja/fact test method to perform the GET request

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


StatusCode        : 200
StatusDescription : OK
Content           : {"fact":"A cat's hearing is much more sensitive than humans and dogs.","length":60}
RawContent        : HTTP/1.1 200 OK
                    Transfer-Encoding: chunked
                    Connection: keep-alive
                    Vary: Accept-Encoding
                    X-RateLimit-Limit: 300
                    X-RateLimit-Remaining: 299
                    X-Frame-Options: SAMEORIGIN
                    X-XSS-Protection: 1; mode=b...
Forms             : {}
Headers           : {[Transfer-Encoding, chunked], [Connection, keep-alive], [Vary, Accept-Encoding],
                    [X-RateLimit-Limit, 300]...}
Images            : {}
InputFields       : {}
Links             : {}
ParsedHtml        : mshtml.HTMLDocumentClass
RawContentLength  : 83
Powershell Invoke WebRequest POST request

Powershell Invoke-WebRequest POST request

Invoke-WebRequest POST JSON request can be defined as below,

Below is an equivalent body parameter example in Powershell,

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

Below is the POST method example using Invoke-WebRequest,

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

Powershell Invoke-WebRequest POST request with Headers

Powershell invoke-webrequest headers can be defined as below,

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

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

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

Powershell Invoke-WebRequest POST request with Authentication headers

APIs require you to authenticate via different authentication schemes like,

  • JWT Bearer Authentication
  • Basic authentication
  • OAuth token

You can call Invoke-WebRequest POST request with Authentication headers like sending secured tokens using JWT bearer or OAuth etc.

JWT Bearer Authentication

invoke-webrequest headers example is as below,

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

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

Invoke-WebRequest -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 *