How to delete a branch using Bitbucket REST API
Asked Answered
R

2

7

Using postman, I have succeeded in creating branches in bitbucket via their REST API and using the guide below

Bitbucket Rest API Guide

But I cannot delete a branch using the same guide as well.

I have created a JSON and placed it in the body tab of POSTMAN then using DELETE for the HTTP method but no success.

I am getting error 405 - Method Not Allowed, what does that mean? Did my request pushed through but I am not allowed?

Using the bitbucket web UI, I am able to delete and create branches.

Edit:

This is the postman generated CURL

curl -X DELETE https://<url>/rest/api/1.0/projects/<abc>/repos/<xyz>/branches 
-H 'authorization: Bearer xxxxxx' -H 'cache-control: no-cache' 
-H 'content-type: application/json'  
-H 'x-atlassian-token: nocheck' 
-d '{"name": "refs/heads/feature/name","dryRun": false}'
Rosewood answered 24/4, 2018 at 16:46 Comment(6)
You should read the docs to understand the error: ”The request HTTP method is not appropriate for the targeted resource. For example an HTTP GET to a resource that only accepts an HTTP POST will result in a 405.”Hawks
You've linked to the Bitbucket Server API, which indicates that DELETE is a valid method. Are you trying to use that against Bitbucket Cloud (bitbucket.org)?Direct
I am using it for bitbucket serverRosewood
Can you give examples of the exact URL you're trying to issue a DELETE against? It would be even better if you could provide a curl example to reproduce the issueHulbard
as Dave says -- what exactly is the URL you are hitting? The body should be irrelevant, I think.Falciform
i have updated the post with the CURL script that was generated by POSTMANRosewood
H
13

It looks like you're using the incorrect REST endpoint, one that doesn't accept the DELETE HTTP verb. The one you're using is:

/rest/api/1.0/projects/{projectKey}/repos/{repositorySlug}/branches

As per the docs, this endpoint only accepts GETs and POSTs

Judging by the format of your call, I'm guessing the one you're actually after is this:

/rest/branch-utils/1.0/projects/{projectKey}/repos/{repositorySlug}/branches

The branch-utils API docs describe more or less the exact payload you're trying to use.

Digging a little deeper and I believe this mistake isn't your fault. The Bitbucket branch util docs for Bitbucket 5.8 and below show the correct URL path, but for 5.9 and up the path seems to be missing.

Full disclosure: I work for Atlassian. That being the case I'll chase this up and have it corrected :)

Edit: the docs have since been fixed!

Hulbard answered 13/5, 2018 at 10:59 Comment(2)
oh my gosh! you are correct! thank you for pointing that out! and yes it worked!Rosewood
@Hulbard I know this is older, but kudos for the initiative and using your internal influence to make a difference in public docs!Kluge
N
1

The accepted answer gives the BB 1.0 API request details. In the 2.0 API this is really simple. It's just an empty delete request with no payload to:

DELETE {{BB_API}}/repositories/{workspace}/{reposlug}/refs/branches/{branchname}

So for example if you have repo "myrepo" in project "sandbox", to delete the branch named "test1" you'd do (excluding auth)

curl --location --request DELETE 'https://api.bitbucket.org/2.0/repositories/sandbox/myrepo/refs/branches/test1'
Naman answered 1/11, 2022 at 21:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.