Today, let us explore a way to get JSON data from a URL using two methods.
- Powershell wget – The wget command usage in Powershell. This is useful when you want to fetch some data from a remote JSON URL(HTTP or HTTPS) from within your Powershell script.
- Powershell Invoke-WebRequest – this is a PowerShell cmdlet, which can send requests to remote URLs, similar to the above method, and can handle SSL and non-SSL requests.
Powershell wget, getting some JSON data.
The command format: wget <options> url
Example:
PS C:\Users\tech> wget https://dummyjson.com/products/1
StatusCode : 200
StatusDescription : OK
Content : {"id":1,"title":"iPhone 9","description":"An apple mobile which is nothing like apple","price":549,"discountPerc
entage":12.96,"rating":4.69,"stock":94,"brand":"Apple","category":"smartphones","thumbna...
RawContent : HTTP/1.1 200 OK
Connection: keep-alive
Access-Control-Allow-Origin: *
X-Dns-Prefetch-Control: off
X-Frame-Options: SAMEORIGIN
Strict-Transport-Security: max-age=15552000; includeSubDomains
X-Dow...
Forms : {}
Headers : {[Connection, keep-alive], [Access-Control-Allow-Origin, *], [X-Dns-Prefetch-Control, off], [X-Frame-Options,
SAMEORIGIN]...}
Images : {}
InputFields : {}
Links : {}
ParsedHtml : System.__ComObject
RawContentLength : 525
Powershell wget using the native method Invoke-WebRequest
Here is the native way of getting a remote URL inside Powershell and some JSON data.
PS C:\Users\tech> Invoke-WebRequest -Uri https://dummyjson.com/products/1
StatusCode : 200
StatusDescription : OK
Content : {"id":1,"title":"iPhone 9","description":"An apple mobile which is nothing like apple","price":549,"discountPerc
entage":12.96,"rating":4.69,"stock":94,"brand":"Apple","category":"smartphones","thumbna...
RawContent : HTTP/1.1 200 OK
Connection: keep-alive
Access-Control-Allow-Origin: *
X-Dns-Prefetch-Control: off
X-Frame-Options: SAMEORIGIN
Strict-Transport-Security: max-age=15552000; includeSubDomains
X-Dow...
Forms : {}
Headers : {[Connection, keep-alive], [Access-Control-Allow-Origin, *], [X-Dns-Prefetch-Control, off], [X-Frame-Options,
SAMEORIGIN]...}
Images : {}
InputFields : {}
Links : {}
ParsedHtml : System.__ComObject
RawContentLength : 525
Notes:
We have used Windows Powershell inside Windows-Terminal and tried using PowerShell wget and Invoke-WebRequest.
Please feel free to ask.