尝试使用来自 Java 客户端的 REST API 更新 VSO 上的工作项时收到 400(错误请求)
Getting 400 (bad request) when trying to update Work Item on VSO with REST API from Java client
我正在尝试使用 REST 更改(更新)Visual Studio Online 工作项(User Story 类型) API (https://www.visualstudio.com/en-us/docs/integrate/api/wit/work-items#update-work-items)
private static String addCommenttoWhosebug() 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)。
我已经成功地使用非常相似的代码进行查询,但无法更新。
参考下面这段代码:
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);
}
我正在尝试使用 REST 更改(更新)Visual Studio Online 工作项(User Story 类型) API (https://www.visualstudio.com/en-us/docs/integrate/api/wit/work-items#update-work-items)
private static String addCommenttoWhosebug() 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)。 我已经成功地使用非常相似的代码进行查询,但无法更新。
参考下面这段代码:
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);
}