Search code examples
bitbucketbitbucket-api

Unable to create a new branch in a repo using Bitbucket API 2.0


I want to create a new branch named test-branch-name from the master branch in a given repository. So as per the documentation provided here, I tried creating a new branch in the repository using Postman.

curl --location --request POST 'https://api.bitbucket.org/2.0/repositories/<workspace>/<repo_name>/refs/branches' \
--header 'Content-Type: application/json' \
--header 'Authorization: Basic <auth_token>' \
--data-raw '{
    "name" : "test-branch-name",
    "target" : {
        "hash" : "default"
    }
}'

With above-mentioned request, I get this response.

{
    "type": "error",
    "error": {
        "fields": {
            "target.hash": "Commit not found: default"
        },
        "message": "Bad request"
    }
}

If, instead of the default hash, I put the full commit hash of the latest commit from the master branch in the request payload, the API call creates the branch from develop branch and not from master.

What am I missing?


Solution

  • The problem is with the JSON data that you pass. Try modifying it as below.

    "{\"name\": \"{BRANCH_NAME}\", \"target\": {\"hash\": \"{SOURCE_BRANCH}\"}}"

    Replace {BRANCH_NAME} with the new branch to be created and {SOURCE_BRANCH} with the source from which it is to be created.