Bitbucket REST API: "Something went wrong" when trying to create a branch restriction
Asked by Nick de Pal?zieux:

I'm trying to use the bitbucket REST api to create a branch restriction rule on a repository according <https://developer.atlassian.com/cloud/bitbucket/rest/api-group-branch-restrictions/#api-repositories-workspace-repo-slug-branch-restrictions-post>.

Here is the python code I'm using:  

```
 url = f"https://api.bitbucket.org/2.0/repositories/{workspace}/{repository}/branch-restrictions"

 headers = {
"Accept": "application/json",
"Content-Type": "application/json",
 }

 payload = json.dumps(
 {
"type": "branchrestriction",
"kind": "push",
"branch_match_kind": "branching_model",
"branch_type": "development",
"users": [ALLOWED_USER], # ALLOWED_USER = "{asdf-asdf-asdf-asdf-asdf}"
 }
 )

 response = requests.request(
"POST",
 url,
data=payload,
headers=headers,
auth=(BBCLI_USERNAME, BBCLI_PASSWORD),
 )
```

The response I get is:

```
500
{'error': {'id': '8f1cbdfd520e48c8b362078b9420c139',
'message': 'Something went wrong'},
'type': 'error'}
```

This happens for repositories that don't have any branch restrictions set up and for ones that have some but not the rule I'm trying to set up.

Listing branch permissions works fine.

What's wrong with my request?


Answers:
author: Patrik S
body: Hey [@Nick de Pal?zieux](/t5/user/viewprofilepage/user-id/1589717) ,

Thank you for reaching out to Atlassian Support.

Accordingly, to the API reference for the[Create branch restriction endpoint](https://developer.atlassian.com/cloud/bitbucket/rest/api-group-branch-restrictions/#api-repositories-workspace-repo-slug-branch-restrictions-post), I think the syntax you have used for defining the users might be incorrect, and thus the error you are facing.

I went ahead and did some tests in my repository and was able to successfully create a branch restriction using the following example request body :

```
{

  "kind": "push",

  "branch_match_kind": "branching_model",

  "branch_type": "development",

  "users": [

    {

      "username": "bitbucket_username_1"
    },
    {

      "username": "bitbucket_username_2"

    }

  ]

}
```

The resulting complete curl command is the below :

```
curl --request POST --user USERNAME:APP_PASSWORD --url https://api.bitbucket.org/2.0/repositories/<WORKSPACE>/<REPOSITORY>/branch-restrictions --header 'Accept: application/json' --header 'Content-Type: application/json' --data '{

  "kind": "push",

  "branch_match_kind": "branching_model",

  "branch_type": "development",

  "users": [

    {

      "username": "bitbucket_username_1"
    },
    {

      "username": "bitbucket_username_2"

    }

  ]

}'
```

You can try using the command above and check how it goes, and then adapt it to your python script syntax.

Hope that helps! Let me know in case you run into any issues.

Thank you, [@Nick de Pal?zieux](/t5/user/viewprofilepage/user-id/1589717) .

Kind regards,

Patrik S

comments: author: Nick de Pal?zieux
body: Hi [@Patrik S](/t5/user/viewprofilepage/user-id/4603309)

Thanks for your response. You're right that the problem was the way I was specifying the user. I don't find the documentation to be clear enough.

Trying your suggestion with a single user works. However, your example of specifying two users seems wrong, as you are providing a dictionary with two entries that have the same key \`username\`.

Should it instead be

```
"users": [

    { "username": "bitbucket_username_1" },

    { "username": "bitbucket_username_2" }

  ]
```

?


author: Patrik S
body: Hey [@Nick de Pal?zieux](/t5/user/viewprofilepage/user-id/1589717) ,

Happy to hear that it worked!

As for the syntax to add more than one user, you're right! I've updated my previous answer to fix the syntax, thank you for the heads-up!

Kind regards,

Patrik S


Tags: bitbucket-cloud

branch

cloud

permission

rest-api