【问题标题】:Getting 400 (bad request) when trying to update Work Item on VSO with REST API from Java client尝试使用 Java 客户端的 REST API 更新 VSO 上的工作项时收到 400(错误请求)
【发布时间】:2018-04-04 07:40:12
【问题描述】:

我正在尝试使用 REST API (https://www.visualstudio.com/en-us/docs/integrate/api/wit/work-items#update-work-items) 更改(更新)Visual Studio Online 工作项(User Story 类型)

private static String addCommenttoStackOverflow() throws IOException {
    String url = "https://<myproject>.visualstudio.com/DefaultCollection/_apis/wit/workitems/<specific id>?api-versoin=1.0";
    HttpUriRequest request = RequestBuilder.patch(url)
            .setHeader(HttpHeaders.CONTENT_TYPE, "application/json-patch+json")
            .setHeader("Authorization", "Basic " + getVsoAuthenticationString())
            .setEntity(EntityBuilder.create()
                    .setContentEncoding("UTF8")
                    .setText("[{\"op\":\"add\",\"path\":\"/fields/System.History\",\"value\":\"JavaScript implementation for Microsoft Account\"}]")
                    .build())
            .build();


    try(CloseableHttpClient httpclient = HttpClients.createDefault()) {
        try(CloseableHttpResponse response = httpclient.execute(request)) {

            System.out.println("Status = " + response.getStatusLine());
            HttpEntity entity =response.getEntity();
            System.out.println(entity.toString());
            System.out.println(entity.getContent().toString());
            ResponseHandler<String> handler = new BasicResponseHandler();
            return handler.handleResponse(response);
        }
    }
}

我将 Apache HttpComponents 用于 Java REST 客户端和基本基本身份验证 (PAT)。 我已成功使用非常相似的代码进行查询,但无法更新。

【问题讨论】:

    标签: visual-studio rest azure-devops apache-httpcomponents


    【解决方案1】:

    参考以下代码:

    byte[] encodedBytes = Base64.getEncoder().encode("test:{your personal access token}".getBytes());
                String basedToken=new String(encodedBytes);
                HttpClient httpClient = HttpClientBuilder.create().build();
    
                HttpPatch patchRequest=new HttpPatch("https://{account}.visualstudio.com/DefaultCollection/_apis/wit/workitems/{work item id}?api-version=1.0");
                StringEntity input = new StringEntity("[{\"op\":\"add\",\"path\":\"/fields/System.History\",\"value\":\"JavaScript implementation for Microsoft Account2\"}]");
                input.setContentType("application/json-patch+json");
                patchRequest.setEntity(input);
                patchRequest.setHeader("Authorization", "Basic "+basedToken);
    
                HttpResponse response = httpClient.execute(patchRequest);
    
    
    
                BufferedReader br = new BufferedReader(
                                new InputStreamReader((response.getEntity().getContent())));
    
                String output;
                System.out.println("Output from Server .... \n");
                while ((output = br.readLine()) != null) {
                    System.out.println(output);
                }
    

    【讨论】:

    猜你喜欢
    • 2015-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-04
    • 2014-03-29
    • 2021-12-14
    • 2015-08-17
    • 1970-01-01
    相关资源
    最近更新 更多