Differences Invoke-WebRequest Vs Invoke-RestMethod
Today in this article we shall see few differences between the widely used PowerShell utility methods Invoke-WebRequest and Invoke-RestMethod.
Today in this article, we will cover below aspects,
Invoke-WebRequest
- This cmdlet was introduced in PowerShell 3.0.
- Efficiently deal with HTML content
- The
Invoke-WebRequest
cmdlet sends HTTP and HTTPS and also supports FTP etc. requests to a web page or web service.
- Support REST API calls, Web calls, and all common HTTP verbs dealing with resources.
- This cmdlet returns the raw HTTP response. The response includes headers, status codes, and the content of the response. It is up to the user to manually parse the data if needed.
- While it can be used for APIs, it requires additional steps to manually parse the response data.
- It can lets you collect links, images, and other HTML elements as per your requirements.
- Beginning in PowerShell 7.0,
Invoke-WebRequest
uses HttpClient.DefaultProxy Property to determine the proxy configuration.
Below is a sample Invoke-WebRequest request and its response,
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
Invoke-RestMethod
- This cmdlet is introduced in Windows PowerShell 3.0.
- Efficiently deal with JSON and XML content.
- The
Invoke-RestMethod
sends HTTP and HTTPS requests to REST(Representational State Transfer ) web services.
- PowerShell formats the response based on the data type. This cmdlet automatically parses the response data (usually JSON or XML) and converts it into a PowerShell object, making it easier to work with the data returned from the RESTful API.
- Easier to work with RESTful APIs. The parsed response can be used as PowerShell objects, simplifying data read and update.
- Beginning in PowerShell 7.0
Invoke-RestMethod
supports proxy configuration defined by environment variables. It uses HttpClient.DefaultProxy Property to determine the proxy configuration.
Below is sample Invoke-Restmethod request and its response,
PS C:\Test\File\load-sqldata> Invoke-Restmethod https://catfact.ninja/fact
fact
----
If a cat is frightened, the hair stands up fairly evenly all over the body; when the cat is threatened or is ready t...
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.