Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

POST to 'Downloads' failing - Bad Request (400).

katsuragi545 August 3, 2017

I'm trying to perform a POST using Invoke-WebRequest using a Powershell script. I'm getting a Bad Request (400) error which from my understanding means there is something wrong with the way I'm doing the POST. Here is my Powershell script:

#Create a credential variable for account$cred = Get-Credential$postParams = @{files='.\Hello.txt'}try {
    Invoke-WebRequest -Credential $cred -Uri https://api.bitbucket.org/2.0/repositories/myBitbucketUsername/myRepoName/downloads -ContentType "multipart/form-data" -Method POST -Body $postParams}catch {echo '### Inside catch ###'    $ErrorMessage = $_.Exception.Message     echo '## ErrorMessage ##' $ErrorMessage
    $FailedItem = $_.Exception.ItemName     echo '## FailedItem ##' $FailedItem 
    $result = $_.Exception.Response.GetResponseStream()     echo '## result2 ##' $result
    $reader = New-Object System.IO.StreamReader($result)     echo '## reader ##' $reader 
    $responseBody = $reader.ReadToEnd();     echo '## responseBody ##' $responseBody}pause

Here is the info I get from the catch block when executing the script:

### Inside catch ###
## ErrorMessage ##
The remote server returned an error: (400) Bad Request.
## FailedItem ##
## result2 ##


CanTimeout   : True
ReadTimeout  : -1
WriteTimeout : -1
CanRead      : True
CanSeek      : True
CanWrite     : True
Capacity     : 256
Length       : 26
Position     : 26

## reader ##
CurrentEncoding : System.Text.UTF8EncodingBaseStream      : System.Net.SyncMemoryStream
EndOfStream     : True

## responseBody ##    

I'm unsure of what I'm doing wrong. The file I am trying to POST (Hello.txt), is located on the Desktop which also contains the Powershell script. 

0 answers

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events