【问题标题】:Upload an attachment to azure devops [REST API]将附件上传到 azure devops [REST API]
【发布时间】:2020-09-13 23:02:04
【问题描述】:

我很难通过 api 在我的 azure devops repo 中添加附件...

public static void putAttachments(Integer id) {
        try {
            URL url = new URL(
                    "https://dev.azure.com/marcoparra0034/AgileFr/_apis/wit/attachments?api-version=5.1&fileName=imageAs.png");
            HttpURLConnection con = ResApiMain.apiConnectionAttachments(PAT, url);
            File file = new File("C:\\Users\\marco.parra\\Pictures\\Screenshots\\new.png");
            String base64Image = encodeFileToBase64Binary(file);

//                  String jsonInputString = "[{\"op\":\"add\",\"path\":\"/fields/System.Title\",\"value\":\"" + "tpain"
//                          + "\"}]";
            base64Image = "[" + base64Image + "]";
            System.out.println("Base xs" + base64Image);

            try (OutputStream os = con.getOutputStream()) {
                byte[] input = Base64.decodeBase64(base64Image.getBytes("utf-8"));
                System.out.println(new String(input));
                os.write(input, 0, input.length);
            } catch (Exception ex) {
                System.out.println(ex.getMessage());
            }

            try (BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "utf-8"))) {
                StringBuilder response = new StringBuilder();
                String responseLine = null;
                while ((responseLine = br.readLine()) != null) {
                    response.append(responseLine.trim());
                }
                System.out.println(response.toString());
            } catch (Exception ex) {
                System.out.println(ex.getMessage());
            }
            con.disconnect();
        } catch (Exception ex) {

        }

这是连接方式

public static HttpURLConnection apiConnectionAttachments(String PAT, URL url) {
        HttpURLConnection con = null;
        try {
            String AuthStr = ":" + PAT;
            Base64 base64 = new Base64();

            String encodedPAT = new String(base64.encode(AuthStr.getBytes()));
            con = (HttpURLConnection) url.openConnection();
            con.setRequestProperty("Authorization", "Basic " + encodedPAT);
            con.setDoOutput(true);
            System.out.println("URL - " + url.toString());
            System.out.println("PAT - " + encodedPAT);

            // Image Requierements
//          con.setRequestProperty("Content-Type", "image/jpeg");
            con.setDoInput(true);
            con.setUseCaches(false);
            con.setRequestProperty("X-HTTP-Method-Override", "PATCH");
            con.setRequestMethod("POST");

            con.setRequestProperty("Content-Type", "application/octet-stream");
//      con.setRequestProperty("Accept", "application/json");

        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return con;
    }

当我运行它时,它会显示下一个错误代码

Server returned HTTP response code: 405 for URL: https://dev.azure.com/marcoparra0034/AgileFr/_apis/wit/attachments?api-version=5.1&fileName=imageAs.png

更新我了解如何使用 python 和 c#,但我不能按照这个逻辑来创建附件

https://github.com/Microsoft/azure-devops-python-api/blob/1bacd2a3f0128a6d184cf75e2c6f8859d46f270a/vsts/vsts/work_item_tracking/v4_1/work_item_tracking_client.py#L56

期望示例

{
  "id": "a5cedde4-2dd5-4fcf-befe-fd0977dd3433",
  "url": "https://dev.azure.com/fabrikam/_apis/wit/attachments/a5cedde4-2dd5-4fcf-befe-fd0977dd3433?fileName=imageAsFileAttachment.png"
}

https://docs.microsoft.com/en-us/rest/api/azure/devops/wit/attachments/create?view=azure-devops-rest-5.1

任何帮助将不胜感激......

【问题讨论】:

  • 我解决了这个问题注释行 con.setRequestProperty("X-HTTP-Method-Override", "PATCH");
  • 很高兴知道您已经解决了您的问题,您是否介意将您的评论转换为答案,这对阅读此主题的其他社区成员有益,并更容易找到答案。

标签: java api rest azure-devops-rest-api


【解决方案1】:

我通过注释行 con.setRequestProperty("X-HTTP-Method-Override", "PATCH"); 解决了这个问题

【讨论】:

  • 太棒了!感谢您在此处分享您的解决方案,您可以接受它作为答案,这样它可以帮助遇到相同问题的其他社区成员,我们可以归档这个帖子,谢谢。
猜你喜欢
  • 2020-05-25
  • 1970-01-01
  • 1970-01-01
  • 2021-05-12
  • 1970-01-01
  • 2019-05-01
  • 2021-05-14
  • 2020-04-03
  • 2020-06-12
相关资源
最近更新 更多