0

I'm using powershell to work with Bitbucket API. Powershell sends command to check, if pull request could be merged and then merges it.

First command works without any problems:

Invoke-WebRequest -Headers @{Authorization = "Basic $base64AuthInfo"} -Method Get -ContentType "application/json" -Uri "$BaseUrl/rest/api/1.0/projects/$ProjectKey/repos/$RepoSlug/pull-requests/$PullRequestId/merge?version=$PRVersion"

I'm getting a valid JSON response. Example:

{"canMerge":true,"conflicted":false,"outcome":"CLEAN","vetoes":[]}

The second command must use POST request in order to merge the pull request. Same command with Post instead of Get :

Invoke-WebRequest -Headers @{Authorization = "Basic $base64AuthInfo"} -Method Post -ContentType "application/json" -Uri "$BaseUrl/rest/api/1.0/projects/$ProjectKey/repos/$RepoSlug/pull-requests/$PullRequestId/merge?version=$PRVersion" -Verbose

This one returns 400 error without any other information.

VERBOSE: POST https://bitbucket.example.com/rest/api/1.0/projects/TEAM/repos/tmp-test-repo/pull-requests/8/merge?version=2 with 0-byte payload
Invoke-WebRequest : The remote server returned an error: (400) Bad Request.
At line:1 char:1
+ Invoke-WebRequest -Headers @{Authorization = "Basic $base64AuthInfo"} ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

How can make POST request work?

Reference: Bitbucket documentation

Maximilian Burszley
  • 18,243
  • 4
  • 34
  • 63
Dziki_Jam
  • 170
  • 2
  • 10

1 Answers1

0

The problem is that Powershell doesn't output expanded details in Invoke-WebRequest output, so this makes investigation really hard. Turned out that the problem was in account I was using. After I used Fiddler to get the response headers, I got a detailed response from Bitbucket API, so I was able to fix my problem.

Dziki_Jam
  • 170
  • 2
  • 10