使用 java 中的 rest API 删除 Bitbucket 分支
Delete Bitbucket Branch using rest API in java
尝试使用 rest api 删除 bibucket 分支,但总是得到 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方法,也是同样的问题。
java.io.IOException:服务器为 URL 返回 HTTP 响应代码:415:
在 sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1900)
在 sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1498)
在 sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:268)
在 com.bofa.dashboard.DelBitbucketBranch.test(DelBitbucketBranch.java:170)
在 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();
}
}
问题可能是 'method' 正在用于休息呼叫。
您应该使用 DELETE 或 POST 方法,参考下面的 link。
谢谢塔伦!我试过使用 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();
}
}
尝试使用 rest api 删除 bibucket 分支,但总是得到 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方法,也是同样的问题。 java.io.IOException:服务器为 URL 返回 HTTP 响应代码:415: 在 sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1900) 在 sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1498) 在 sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:268) 在 com.bofa.dashboard.DelBitbucketBranch.test(DelBitbucketBranch.java:170) 在 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();
}
}
问题可能是 'method' 正在用于休息呼叫。
您应该使用 DELETE 或 POST 方法,参考下面的 link。
谢谢塔伦!我试过使用 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();
}
}