Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell invoke WebRequest with NTLM for a post

INITIAL QUESTION

How Can I perform Invoke-WebRequest or similar, with Powershell so that NTLM authentication is used but also supply a body for a post.

EXAMPLE

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"

UPDATE

Using -UseDefaultCredentials only works for Gets, not for posts.



ERROR RESPONSE

The remote server returned an error: (401) Unauthorized

like image 609
Murchie85 Avatar asked Jan 26 '18 15:01

Murchie85


People also ask

What is PowerShell invoke-WebRequest?

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.

What is the difference between invoke-RestMethod and invoke-WebRequest?

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.

What is Uri in invoke-WebRequest?

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.

What is invoke REST method?

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.


1 Answers

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...

like image 115
markgamache Avatar answered Sep 17 '22 16:09

markgamache