Setting the default branch with REST API

Eyal Goren March 30, 2020

Hi,

I am trying to create a Jenkins Job that automate the creation of Repository in Bitbucket.

I am facing issues with switching default branch using the REST API-

When I call the API with GET- I am receiving the correct value, but when I try to use the POST for setting value, I don't get any errors, but the default branch is not set.

This is what I try to call-

curl --user <user>:<password> -H "Content-Type: application/json" --data '{ "id": "<New branch>" }' -X POST https://<host>/rest/api/1.0/projects/<Project key>/repos/<repo name>/branches/default

2 answers

1 accepted

0 votes
Answer accepted
Dave Chevell
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
April 12, 2020

Hey Eyal_Goren,

 

The short answer is that you're using the wrong HTTP verb - you should be using PUT, not POST.

More generally, in REST API semantics POST is typically used for creating new resources (e.g. the API to create a branch uses POST), whereas PUT is used for updating existing resources (e.g. in this case, where we're changing the default branch but not creating anything new).

See also https://docs.atlassian.com/bitbucket-server/rest/7.1.1/bitbucket-rest.html#idp205

Eyal Goren April 15, 2020

Thanks, now it worked!!

Like hosssamezzoukh likes this
3 votes
Hari Sekhon
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
March 31, 2022

It looks like the v2 API doesn't work to set the branch at all:

 

curl ... https://api.bitbucket.org/2.0/repositories/harisekhon/devops-bash-tools -H 'Accept: application/json' -H 'Content-Type: application/json' -X PUT -d '{"mainbranch": { "name": "somexistingbranch", "type": "branch" } }'

 

returns a 200 OK but doesn't change the default branch.

 

This looks like a bug?

Nick de Palézieux
Contributor
July 30, 2024

Seeing the same thing. Have you found a solution?

Kevin De Notariis
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
December 30, 2024

Well, they still didn't implement it.... 

 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events