删除 Bitbucket 分支使用 REST API 在 Java 中。

huangapple go评论67阅读模式
英文:

Delete Bitbucket Branch using rest API in java

问题

尝试使用 REST API 删除 Bitbucket 分支,但总是得到 405、415 或 500 的响应代码。以下是代码片段,请指导!

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());

同时尝试了 Postman,以下是错误信息。任何帮助将不胜感激!

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

我还尝试了使用 POST 方法,结果也出现了相同的问题。

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();
    }
}
英文:

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 = &quot;BASE_URL/rest/branch-utils/1.0/projects/&lt;PROJECT&gt;/repos/&lt;REPO&gt;/branches&quot;;
String requestJson = &quot;{\&quot;name\&quot;:\&quot;refs/heads/feature/TEST\&quot;,\&quot;dryRun\&quot;:\&quot;false\&quot;}&quot;;
HttpClient client = HttpClientBuilder.create().build();
HttpPut putRequest = new HttpPut(endPoint);
putRequest.addHeader(&quot;accept&quot;, &quot;application/json&quot;);
putRequest.addHeader(AUTHORIZATION, BASIC + &quot;passwordencrypted&quot;);
StringEntity input = new StringEntity(requestJson, StandardCharsets.UTF_8);
input.setContentType(&quot;application/json&quot;);
putRequest.setEntity(input);
HttpResponse response = client.execute(putRequest);
System.out.println(&quot;StatusCode :: &quot; + 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(&quot;FINAL :: &quot; + result.toString());

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

{
&quot;errors&quot;: [
{
&quot;context&quot;: null,
&quot;message&quot;: &quot;An error occurred while processing the request. Check the server logs for more information.&quot;,
&quot;exceptionName&quot;: 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 = &quot;BASE_URL/rest/branch-utils/1.0/projects/&lt;PROJECT&gt;/repos/&lt;REPO&gt;/branches&quot;;
URL url = new URL(endPoint);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod(&quot;POST&quot;);
conn.setRequestProperty(&quot;Accept&quot;, &quot;application/json&quot;);
conn.setRequestProperty(&quot;Authorization&quot;, &quot;Basic &quot; + passwordencrpted);
conn.setDoOutput(true);
conn.setDoInput(true);
String requestJson = &quot;{\&quot;name\&quot;:\&quot;refs/heads/feature/TEST\&quot;,\&quot;dryRun\&quot;:\&quot;false\&quot;}&quot;;
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();
}
}

答案1

得分: 4

问题可能出在用于 REST 调用的 'method'。

您应该使用 DELETE 或 POST 方法,请参考下面的链接。

Bitbucket Server 提供的 REST 资源 - 分支

英文:

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

得分: 2

感谢Tarun!我已经尝试了Delete方法,它能够正常工作,但是它没有提供任何响应返回。似乎Delete方法没有返回任何内容。以下是可工作的代码片段:

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();
    }
}
英文:

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 删除 Bitbucket 分支使用 REST API 在 Java 中。

public static void test() throws JSONException {
try {
String endPoint = &quot;BASE_URL/rest/branch-utils/1.0/projects/&lt;PROJECT&gt;/repos/&lt;REPO&gt;/branches&quot;;
URL url = new URL(endPoint);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod(&quot;DELETE&quot;);
conn.setRequestProperty(&quot;Content-Type&quot;, &quot;application/json&quot;);
conn.setRequestProperty(&quot;Authorization&quot;, &quot;Basic &quot; + passwordencrpted);
conn.setDoOutput(true);
conn.setDoInput(true);
String requestJson = &quot;{\&quot;name\&quot;:\&quot;refs/heads/feature/TEST\&quot;,\&quot;dryRun\&quot;:\&quot;false\&quot;}&quot;;
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();
}
}

huangapple
  • 本文由 发表于 2020年7月25日 14:46:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/63085251.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定