1
votes

Trying to delete bibucket branch using rest api but always getting 405 or 415 or 500 as response code. Given below code snippet. Please guide!

String endPoint = "BASE_URL/rest/branch-utils/1.0/projects/<PROJECT>/repos/<REPO>/branches";
String requestJson = "{\"name\":\"refs/heads/feature/TEST\",\"dryRun\":\"false\"}";
HttpClient client = HttpClientBuilder.create().build();
HttpPut putRequest = new HttpPut(endPoint);
putRequest.addHeader("accept", "application/json");
putRequest.addHeader(AUTHORIZATION, BASIC + "passwordencrypted");

StringEntity input = new StringEntity(requestJson, StandardCharsets.UTF_8);
input.setContentType("application/json");
putRequest.setEntity(input);

HttpResponse response = client.execute(putRequest);
System.out.println("StatusCode :: " + response.getStatusLine().getStatusCode());

BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));
String output;
StringBuffer result = new StringBuffer();
while ((output = br.readLine()) != null) {
result.append(output);
}
System.out.println("FINAL :: " + result.toString());

Also tried with Postman, below errors. Any help will be appreciated!

{
"errors": [
{
"context": null,
"message": "An error occurred while processing the request. Check the server logs for more information.",
"exceptionName": null
}
]
}

I have tried with Post method as well, it is also causing the same issue. java.io.IOException: Server returned HTTP response code: 415 for URL: at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1900) at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1498) at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:268) at com.bofa.dashboard.DelBitbucketBranch.test(DelBitbucketBranch.java:170) at com.bofa.dashboard.DelBitbucketBranch.main(DelBitbucketBranch.java:31)

public static void test() throws JSONException {
        try {

            String endPoint = "BASE_URL/rest/branch-utils/1.0/projects/<PROJECT>/repos/<REPO>/branches";
            URL url = new URL(endPoint);

            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Accept", "application/json");
            conn.setRequestProperty("Authorization", "Basic " + passwordencrpted);
            conn.setDoOutput(true);
            conn.setDoInput(true);

            String requestJson = "{\"name\":\"refs/heads/feature/TEST\",\"dryRun\":\"false\"}";
            DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
            wr.write(requestJson.getBytes());

            InputStream inputStream = conn.getInputStream();
            ByteArrayOutputStream result = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int length;

            while ((length = inputStream.read(buffer)) != -1) {
                result.write(buffer, 0, length);
            }
            String jsonStr = result.toString(UTF_8);

            System.out.println(jsonStr);

            wr.flush();
            wr.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (ProtocolException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
2

2 Answers

4
votes

The Problem may be the 'method' which is being used for rest call.

You should use DELETE or POST method, Refer link below.

REST Resources Provided By: Bitbucket Server - Branch

2
votes

Thanks Tarun! I have tried with Delete method, it works fine but it doesn't provide any response back. It seems Delete is not returning any content. Working piece of code :)

public static void test() throws JSONException {
        try {

            String endPoint = "BASE_URL/rest/branch-utils/1.0/projects/<PROJECT>/repos/<REPO>/branches";
            URL url = new URL(endPoint);

            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("DELETE");
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setRequestProperty("Authorization", "Basic " + passwordencrpted);
            conn.setDoOutput(true);
            conn.setDoInput(true);

            String requestJson = "{\"name\":\"refs/heads/feature/TEST\",\"dryRun\":\"false\"}";
            DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
            wr.write(requestJson.getBytes());

            InputStream inputStream = conn.getInputStream();
            ByteArrayOutputStream result = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int length;

            while ((length = inputStream.read(buffer)) != -1) {
                result.write(buffer, 0, length);
            }
            String jsonStr = result.toString(UTF_8);

            System.out.println(jsonStr);

            wr.flush();
            wr.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (ProtocolException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }