Reputation: 3626
I'm trying to POST a file to the 'Downloads' section of a Bitbucket repository. I'm following this page from Bitbucket's documentation. I'm trying to do this by writing a Powershell script. I've managed to get the GET method to work with:
#Invoke-RestMethod -Credential $cred -Uri https://api.bitbucket.org/2.0/repositories/myBitbucketUsername/myBitbucketRepoName/downloads?access_token=9EtOz9JDeWGwxTLKx3ya2oPE8g652GoLN0cMmtD0Ncvkf2OXoio0bcXwSigNE9AXTT2aj6qmbS5XHae7rIc%3D"&"scopes=pipeline%3Avariable+webhook+snippet%3Awrite+wiki+issue%3Awrite+pullrequest%3Awrite+repository%3Adelete+repository%3Aadmin+project%3Awrite+team%3Awrite+account%3Awrite"&"expires_in=3600 -OutFile .\file.txt
In order to get that GET to work I had to add the access token which I made through Bitbucket (hence all the text for the access token portion of the Uri).
I'm not sure how to perform the POST. I've tried several things including something similar to the documentation:
curl -s -u myBitbucketUsername -X POST https://api.bitbucket.org/2.0/repositories/myBitbucketUsername/myBitbucketRepoName/downloads -F [email protected]
The error I get inside Powershell when running this command is:
Invoke-WebRequest : Parameter cannot be processed because the parameter name 'u' is ambiguous. Possible matches include:
-UseBasicParsing -Uri -UseDefaultCredentials -UserAgent.
At C:\Users\user\Desktop\ScriptTest.ps1:21 char:9
+ curl -s -u myBitbucketUsername-X POST https://api.bitbucket.org/2.0/reposit ...
+ ~~
+ CategoryInfo : InvalidArgument: (:) [Invoke-WebRequest], ParameterBindingException
+ FullyQualifiedErrorId : AmbiguousParameter,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
I've also tried with the access token added to the URL (similar to how I did the GET).
The file I am trying to POST is 'Hello.txt' which is on my Desktop - the same location as my Powershell script.
Any ideas?
EDIT:
I've followed briantist's advice and switched to using credentials instead. Now, I get a 'Bad Request (400)
' error. Here is my code:
$postParams = @{files='.\Hello.txt'}
Invoke-WebRequest -Credential $cred -Uri https://api.bitbucket.org/2.0/repositories/myBitbucketUsername/myRepoName/downloads -ContentType "multipart/form-data" -Method POST -Body $postParams
Upvotes: 0
Views: 1500
Reputation: 47802
curl
in PowerShell is an alias for Invoke-WebRequest
(and so is wget
). This was a mistake; they should not have done this, because the parameters are not the same and Invoke-WebRequest
, while doing something similar, is not a drop-in replacement.
The error is telling you what the problem is: -u
is ambiguous for Invoke-WebRequest
. It has 3 parameters that could possibly be, and it's telling you what they are.
But critically, you're trying to use it as a user name, which Invoke-WebRequest
doesn't accept at all (it takes a [PSCredential]
object, not a separate plaintext user name and password).
So, you should look at how to use credentials with Invoke-WebRequest
, or use curl
directly, if you have it and want to use it.
One way to get around the alias is to call it with extension: curl.exe
Another way is to remove the alias: Remove-Alias -Name curl
Upvotes: 2