英文:
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 = "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();
}
}
答案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.
答案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 ![]()
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();
}
}
专注分享java语言的经验与见解,让所有开发者获益!

评论