Cannot merge pull request via Bitbucket REST api when body is provided

Hi,

I am trying to merge a pull request through the REST Api. This works when I do not provide any body. However I would like to set the merge strategy so I am trying to provide the body as specified in the documentation.

According to the documentation the body should look like:

{
	"type": "pullrequest",
	"message": "My message",
	"close_source_branch": true,
	"merge_strategy": "merge_commit"
}

This does not work. I am getting a bad request error without any details. Somewhere else on the developer community I saw the following example (which seemed to work for the person who posted it):

{
	"pullrequest_merge_parameters": {
		"type": "pullrequest",
		"message": "My message",
		"close_source_branch": true,
		"merge_strategy": "merge_commit"
	}
}

But this does not work either. I am sure the PR can be merged and has no merge conflicts. I can check this via the GUI but also when I leave the body out of the request the PR is merged.

What am I doing incorrectly? Any help appreciated!

regards,

Jarno

1 Like

Just to confirm, you mean that you have tried doing this via console?

await fetch("https://bitbucket.org/!api/2.0/repositories/Farjad/testrepo/pullrequests/2/merge", {
    "credentials": "include",
    "headers": {
        "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:101.0) Gecko/20100101 Firefox/101.0",
        "Accept": "*/*",
        "Accept-Language": "en-US,en;q=0.5",
        "X-Requested-With": "XMLHttpRequest",
        "X-Bitbucket-Frontend": "frontbucket",
        "X-Bitbucket-Destination": "microsbucket",
        "X-CSRFToken": "__CORS_TOKEN_HERE___",
        "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
        "Sec-Fetch-Dest": "empty",
        "Sec-Fetch-Mode": "cors",
        "Sec-Fetch-Site": "same-origin"
    },
    "referrer": "https://bitbucket.org/Farjad/testrepo/pull-requests/2",
    "body": '{"type": "pullrequest","message": "My message","close_source_branch": true,"merge_strategy": "merge_commit"}',
    "method": "POST",
    "mode": "cors"
});

:point_up_2: That works as expected via the browser console.

Hi bentley,

No I have not tried this via the browser console. What I meant by checking via the GUI is actually merging the issue via the GUI page of Bitbucket. That works. If I create a similar issue branch that is to be merged again and try via Postman (or via my Java code) then it fails. I will check the browser console option to see if that works. I don’t have time today and I am going vacation for a week so I’ll check after my vacation and let you know if that works or not. Thanks for your quick response!

regards,

Jarno

So I tried your browser option but at first it failed with a 403 because I guess I had the CSRF token incorrect. After fixing that I got a 404 even though I am pretty sure I got the workspacename and repo name correctly.

Then I tried this using cURL and that worked. Then tried it again via Java and it didn’t work. Difference turned out to be the Content-Type header. It was not being passed correctly from Java and should have been set to application/json. After fixing this from Java it works.

From Postman it still does not even though I have the header set correctly. I still get a 400 bad request. But as long as this works from my Java tool I am happy.

So turns out if you do not put the correct content type in the header then the body will be ignored by Bitbucket.

1 Like