无法在 TFS 中使用批处理操作创建批处理工作项
Cannot create batch work items using batch operations in TFS
我在尝试使用批量创建方法创建工作项时出现以下错误
错误 1
"Message":"No MediaTypeFormatter is available to read an object of type 'JsonBatchHttpRequest' from content with media type 'application/json-patch+json'."
错误 2
{"count":1,"value":{"Message":"One or more errors occurred."}}
我已参考此文档https://www.visualstudio.com/en-us/docs/integrate/api/wit/batch from Microsoft . and my question on stackoverflaw
我试过用以下方法发送数据
- "json: x"
- "body: x:"
- "body:JSON.stringify(x)"
- "json:[body:x]"
我已经尝试将 "application/json-patch+json" 和 "application/json"(推荐作为 MIcrosoft 文档)作为 Content-Types
我已经厌倦了 Post(推荐作为 MIcrosoft 文档)和补丁方法
此错误没有可用的参考资料,因此我对此很烂point.What这里可能有误,请帮助..
public batchOperation( ):q.Promise<boolean>{
let deferred = q.defer<boolean>();
try {
var batchCreateUrl = this.collectionURL+"/_apis/wit/$batch?api-version=1.0";
var x= {
method:"PATCH",
uri:"/VSTS_TFS_Test/_apis/wit/workItems/$Bug?api-version=1.0",
headers:{
"Content-Type":"application/json-patch+json"
},
body:[
{ "op":"add",
"path": "/fields/System.Tags",
"value":"tg;tg1;tg2"
},
{
"op": "add",
"path": "/fields/System.Title",
"value": "Some Title Text "
},
{
"op": "add",
"path": "/fields/System.Description",
"value":"this is description"
}
]
}
var options = {
url: batchCreateUrl,
username: this.username,
password: this.password,
domain: this.domain,
method: 'PATCH',
headers: {
'Content-Type': 'application/json-patch+json'
},
body: x
};
httpntlm.patch(options, function(err,res) {
if(err) {
return deferred.reject(false);}
else{
console.log("Patch Complete");
console.log(res.body);
deferred.resolve(true);
}
});
} catch (error) {
console.log("Failed to Perform Batch Operation ")
deferred.reject(false);
}
return deferred.promise;
}
您需要使用 "application/json"
作为内容类型和 post 方法,就像 tutorial of Microsoft documentation 描述的那样。
由于您使用的是 httpntlm,您可以包括以下选项:
- json: if you want to send json directly (content-type is set to
application/json)
- files: an object of files to upload (content-type is set to
multipart/form-data; boundary=xxx)
- body: custom body content you want to send. If used, previous
options will be ignored and your custom body will be sent.
(content-type will not be set)
如果您使用正文,您之前的选项将被忽略(内容类型将丢失),这可能会导致问题。尝试直接使用 json.
我在尝试使用批量创建方法创建工作项时出现以下错误
错误 1
"Message":"No MediaTypeFormatter is available to read an object of type 'JsonBatchHttpRequest' from content with media type 'application/json-patch+json'."
错误 2
{"count":1,"value":{"Message":"One or more errors occurred."}}
我已参考此文档https://www.visualstudio.com/en-us/docs/integrate/api/wit/batch from Microsoft . and my question on stackoverflaw
我试过用以下方法发送数据
- "json: x"
- "body: x:"
- "body:JSON.stringify(x)"
- "json:[body:x]"
我已经尝试将 "application/json-patch+json" 和 "application/json"(推荐作为 MIcrosoft 文档)作为 Content-Types
我已经厌倦了 Post(推荐作为 MIcrosoft 文档)和补丁方法
此错误没有可用的参考资料,因此我对此很烂point.What这里可能有误,请帮助..
public batchOperation( ):q.Promise<boolean>{
let deferred = q.defer<boolean>();
try {
var batchCreateUrl = this.collectionURL+"/_apis/wit/$batch?api-version=1.0";
var x= {
method:"PATCH",
uri:"/VSTS_TFS_Test/_apis/wit/workItems/$Bug?api-version=1.0",
headers:{
"Content-Type":"application/json-patch+json"
},
body:[
{ "op":"add",
"path": "/fields/System.Tags",
"value":"tg;tg1;tg2"
},
{
"op": "add",
"path": "/fields/System.Title",
"value": "Some Title Text "
},
{
"op": "add",
"path": "/fields/System.Description",
"value":"this is description"
}
]
}
var options = {
url: batchCreateUrl,
username: this.username,
password: this.password,
domain: this.domain,
method: 'PATCH',
headers: {
'Content-Type': 'application/json-patch+json'
},
body: x
};
httpntlm.patch(options, function(err,res) {
if(err) {
return deferred.reject(false);}
else{
console.log("Patch Complete");
console.log(res.body);
deferred.resolve(true);
}
});
} catch (error) {
console.log("Failed to Perform Batch Operation ")
deferred.reject(false);
}
return deferred.promise;
}
您需要使用 "application/json"
作为内容类型和 post 方法,就像 tutorial of Microsoft documentation 描述的那样。
由于您使用的是 httpntlm,您可以包括以下选项:
- json: if you want to send json directly (content-type is set to application/json)
- files: an object of files to upload (content-type is set to multipart/form-data; boundary=xxx)
- body: custom body content you want to send. If used, previous options will be ignored and your custom body will be sent. (content-type will not be set)
如果您使用正文,您之前的选项将被忽略(内容类型将丢失),这可能会导致问题。尝试直接使用 json.