I'm using PowerShell Invoke-RestMethod and want to send through the credentials of the currently logged in user. So I'm using the -UseDefaultCredential switch:
Invoke-RestMethod -Uri $serverUrl -UseDefaultCredential -ContentType "application/xml" -Body '<tags count="1"><tag name="testTag" /></tags>' -Method Put -Verbose
But I keep getting a 401 Unauthorised response.
When I create a PSCredentials object and pass that, it works.
$creds = Get-Credential -Credential 'someusername'
Invoke-RestMethod -Uri $serverUrl -Credential $creds -ContentType "application/xml" -Body '<tags count="1"><tag name="testTag" /></tags>' -Method Put -Verbose
Looking at Fiddler I can see both usages get an initial 401 but the option with -Credential sends a follow-up request with an Authorization header.
Why isn't -UseDefaultCredential working? What else can I look at to investigate this issue?
For reference this is sending request to the TeamCity REST API.
Some web servers requires GET before POST. I presume it could be the same with PUT, you could try:
Invoke-RestMethod -Uri $serverUrl -UseDefaultCredential -SessionVariable s -Method Get
Invoke-RestMethod -Uri $serverUrl -WebSession $s -ContentType "application/xml" -Body '<tags count="1"><tag name="testTag" /></tags>' -Method Put -Verbose
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