How Can I perform Invoke-WebRequest or similar, with Powershell so that NTLM authentication is used but also supply a body for a post.
The code sample below is my example post using invoke web request and pipes response out to a .json file. Username and Password Variable not included in example.
$myURL = https://example.blah.etc
$params = @" {""EXAMPLE1":"STUFF"} "@
$Headers = @{ Authorization = "Basic {0}" -f [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $Username,$Password))) }
Invoke-WebRequest -Uri $myURL -Headers $Headers -Method POST -ContentType "application/json" -Body $params | Select-Object -ExpandProperty Content > "C:\output.json"
Using -UseDefaultCredentials only works for Gets, not for posts.
The remote server returned an error: (401) Unauthorized
The Invoke-WebRequest cmdlet sends HTTP and HTTPS requests to a web page or web service. It parses the response and returns collections of links, images, and other significant HTML elements. This cmdlet was introduced in PowerShell 3.0.
Invoke-RestMethod is perfect for quick APIs that have no special response information such as Headers or Status Codes, whereas Invoke-WebRequest gives you full access to the Response object and all the details it provides.
Invoke-WebRequest sends a request to the URI (Uniform Resource Identifier) which is also called Endpoint and retrieves the data from the Web Page. It directly works with the URL or with the REST API because some websites allow modifying the content using only APIs.
Description. The Invoke-RestMethod cmdlet sends HTTP and HTTPS requests to Representational State Transfer (REST) web services that return richly structured data. PowerShell formats the response based to the data type. For an RSS or ATOM feed, PowerShell returns the Item or Entry XML nodes.
just use -UseDefaultCredentials trying to manipulate the headers for NTLM is hard work. It is a challenge response that is painful. Let PS do the work...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With