java 应用程序对 TFS REST API 的 HTTP PATCH 请求收到 400(错误请求)错误
HTTP PATCH request to TFS REST API from java application getting 400 (bad request) error
我正在尝试通过 java 代码向 TFS REST API 发出 HTTP PATCH 请求。但是我得到了 400 错误的请求代码。我的 POST 和 GET 方法从 java.
开始工作
我有以下代码:
public static void patchCenas() throws MalformedURLException, IOException{
URL url = new URL("http://servername:8080/tfs/DefaultCollection/Testing%20Project/_apis/wit/workitems/$Bug?api-version=1.0");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(15000);//15 secs
connection.setReadTimeout(15000);//15 secs
connection.setRequestProperty("X-HTTP-Method-Override", "PATCH");
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "{\"Content-Type\":\"application/json-patch+json\"}");
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
out.write(
"[" +
"{"+
"\"op\":\"add\"," +
"\"path\":\"/fields/System.Title\"," +
"\"value\":\"Bug with PATCH via java (Test)\""+
"}"+
"]");
out.flush();
out.close();
int res = connection.getResponseCode();
System.out.println(connection.getResponseMessage());
System.out.println(res);
BufferedReader br = new BufferedReader(new InputStreamReader(((HttpURLConnection) url.openConnection()).getInputStream(), Charset.forName("UTF-8")));
//InputStream is = connection.getInputStream();
//BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line = null;
while((line = br.readLine() ) != null) {
System.out.println(line);
}
connection.disconnect();
}
消息:
响应消息:错误请求
响应代码:400
行输出:
{
"fields": {
"System.WorkItemType": "Bug",
"System.AreaPath": "Testing Project",
"System.TeamProject": "Testing Project",
"System.IterationPath": "Testing Project",
"System.State": "New",
"System.Reason": "New defect reported",
"Microsoft.VSTS.Common.StateChangeDate": "1753-01-01T00:00:00Z",
"System.ChangedBy": "name>",
"System.CreatedBy": "name>",
"Microsoft.VSTS.Common.Priority": 2,
"Microsoft.VSTS.Common.Severity": "3 - Medium",
"Microsoft.VSTS.Common.ValueArea": "Business"
},
"_links": {
"workItemType": {
"href": "http://servername:8080/tfs/DefaultCollection/xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx/_apis/wit/workItemTypes/Bug"
},
"fields": {
"href": "http://servername:8080/tfs/DefaultCollection/_apis/wit/fields"
}
},
"url": "http://servername:8080/tfs/DefaultCollection/_apis/wit/workItems"
}
我的代码有问题吗?
我测试了您在上面发布的代码片段。内容类型不正确。将其更改为:
connection.setRequestProperty("Content-Type", "application/json-patch+json");
不需要{}
。
我正在尝试通过 java 代码向 TFS REST API 发出 HTTP PATCH 请求。但是我得到了 400 错误的请求代码。我的 POST 和 GET 方法从 java.
开始工作我有以下代码:
public static void patchCenas() throws MalformedURLException, IOException{
URL url = new URL("http://servername:8080/tfs/DefaultCollection/Testing%20Project/_apis/wit/workitems/$Bug?api-version=1.0");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(15000);//15 secs
connection.setReadTimeout(15000);//15 secs
connection.setRequestProperty("X-HTTP-Method-Override", "PATCH");
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "{\"Content-Type\":\"application/json-patch+json\"}");
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
out.write(
"[" +
"{"+
"\"op\":\"add\"," +
"\"path\":\"/fields/System.Title\"," +
"\"value\":\"Bug with PATCH via java (Test)\""+
"}"+
"]");
out.flush();
out.close();
int res = connection.getResponseCode();
System.out.println(connection.getResponseMessage());
System.out.println(res);
BufferedReader br = new BufferedReader(new InputStreamReader(((HttpURLConnection) url.openConnection()).getInputStream(), Charset.forName("UTF-8")));
//InputStream is = connection.getInputStream();
//BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line = null;
while((line = br.readLine() ) != null) {
System.out.println(line);
}
connection.disconnect();
}
消息:
响应消息:错误请求
响应代码:400
行输出:
{
"fields": {
"System.WorkItemType": "Bug",
"System.AreaPath": "Testing Project",
"System.TeamProject": "Testing Project",
"System.IterationPath": "Testing Project",
"System.State": "New",
"System.Reason": "New defect reported",
"Microsoft.VSTS.Common.StateChangeDate": "1753-01-01T00:00:00Z",
"System.ChangedBy": "name>",
"System.CreatedBy": "name>",
"Microsoft.VSTS.Common.Priority": 2,
"Microsoft.VSTS.Common.Severity": "3 - Medium",
"Microsoft.VSTS.Common.ValueArea": "Business"
},
"_links": {
"workItemType": {
"href": "http://servername:8080/tfs/DefaultCollection/xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx/_apis/wit/workItemTypes/Bug"
},
"fields": {
"href": "http://servername:8080/tfs/DefaultCollection/_apis/wit/fields"
}
},
"url": "http://servername:8080/tfs/DefaultCollection/_apis/wit/workItems"
}
我的代码有问题吗?
我测试了您在上面发布的代码片段。内容类型不正确。将其更改为:
connection.setRequestProperty("Content-Type", "application/json-patch+json");
不需要{}
。