将附件上传到 azure devops [REST API] [英] Upload an attachment to azure devops [REST API]

查看:35
本文介绍了将附件上传到 azure devops [REST API]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

public static void putAttachments(Integer id) {尝试 {URL url = 新 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);尝试 (OutputStream os = con.getOutputStream()) {byte[] input = Base64.decodeBase64(base64Image.getBytes("utf-8"));System.out.println(new String(input));os.write(input, 0, input.length);} 捕捉(异常前){System.out.println(ex.getMessage());}尝试 (BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "utf-8"))) {StringBuilder response = new StringBuilder();字符串 responseLine = null;while ((responseLine = br.readLine()) != null) {response.append(responseLine.trim());}System.out.println(response.toString());} 捕捉(异常前){System.out.println(ex.getMessage());}con.disconnect();} 捕捉(异常前){}

这是连接方法

public static HttpURLConnection apiConnectionAttachments(String PAT, URL url) {HttpURLConnection con = null;尝试 {String AuthStr = ":" + PAT;Base64 base64 = new Base64();String encodingPAT = new String(base64.encode(AuthStr.getBytes()));con = (HttpURLConnection) url.openConnection();con.setRequestProperty("授权", "基本" + 编码PAT);con.setDoOutput(true);System.out.println("URL - " + url.toString());System.out.println("PAT - " + 编码PAT);//图像要求//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");} 捕获(异常 e){System.out.println(e.getMessage());}回报骗局;}

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

服务器返回 HTTP 响应代码: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>

期望示例

<代码>{"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");

I'm having a hard time adding a attachments in my azure devops repo via api...

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) {

        }

This is the connection method

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

When i run this it show the next error code

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

Update i see how to work with python and c# but i can´t follow this logic to create an attachment

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

Expectations Example

{
  "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

Any help would be appreciated....

解决方案

I solve this issue commenting line con.setRequestProperty("X-HTTP-Method-Override", "PATCH");

这篇关于将附件上传到 azure devops [REST API]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆