Skip to content

Tag: Invoke-WebRequest

Invoke-WebRequest and Invoke-RestMethod get (401) Unauthorized without any reason

I use PowerShell to access a lot of API’s! Since a while some calls that are correct get the error: “The remote server returned an error: (401) Unauthorized.”.

In my case, this was only the case when I used Windows PowerShell (e.g., PowerShell 5.x)! PowerShell Core (7.x) worked fine.

Upload a file with PowerShell and Invoke-RestMethod

I came across the following challenge: Automate the upload of several build artefacts to the BitBucket cloud service downloads section.
You might ask yourself “why is this a challenge“, and the answer was a little shock for me: “cause it is something that might get very complicated with Windows PowerShell 5“.
I did some test on my Mac and I find it very easy with curl.

curl -X POST "https://MyUsername:[email protected]/2.0/repositories/dummyTeam/myproject/downloads" --form files=@"/home/dev/release\myproject-current.zip"

But curl is not available an my build server. So I tried to get it working with Invoke-RestMethod or Invoke-WebRequest. I ended up very frustrated! Searched around and found a lot of people that seem to have the same problem, but none of the answers seemed to work, at least not for me. Maybe an issue of the BitBucket cloud API, but I think it is more a general issue of Windows PowerShell.

This content is older than 3 years. It might be outdated.