簡體   English   中英

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

問題可能是用於 rest 調用的“方法”。

您應該使用 DELETE 或 POST 方法,請參閱下面的鏈接。

REST 資源提供:Bitbucket 服務器 - 分支

謝謝塔倫,我試過刪除方法。 它工作正常,但沒有提供任何回復。 似乎刪除沒有返回任何內容:工作代碼:)

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM