无法在TFS中使用批处理操作创建批处理工作项

时间:2017-03-20 15:22:52

标签: tfs tfs-workitem azure-pipelines-build-task azure-devops-rest-api

尝试使用批量创建方法

创建工作项时,我遇到了以下错误

错误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."}}

我已经参考了Microsoft的此文档https://www.visualstudio.com/en-us/docs/integrate/api/wit/batch。和我在stackoverflaw上的问题Create Large Amount of Work Items in TFS Using Javascript REST API

我尝试按以下方法发送数据

  • " json:x"
  • " body:x:"
  • "体:JSON.stringify(X)"
  • " JSON:[体:X]"

我已经尝试过" application / json-patch + json"和" application / json"(推荐作为MIcrosoft文档)作为内容类型

我已经厌倦了Post(推荐为MIcrosoft文档)和补丁方法

此错误没有可用的引用,因此我在这一点上很糟糕。这里可能有什么问题请帮助..

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

1 个答案:

答案 0 :(得分:1)

您需要使用"application/json"作为内容类型和发布方法,就像所描述的tutorial of Microsoft documentation一样。

由于您使用的是httpntlm,因此您可以添加以下选项:

  
      
  1. json:如果你想直接发送json(content-type设置为   应用/ JSON)
  2.   
  3. files:要上载的文件对象(content-type设置为   多部分/格式的数据;边界= XXX)
  4.   
  5. 正文:您要发送的自定义正文内容。如果使用,以前   选项将被忽略,您的自定义正文将被发送。   (不会设置内容类型)
  6.         

    Source Link

如果您正在使用正文,您的先前选项将被忽略(内容类型将丢失),这可能会导致问题。直接使用json尝试。

相关问题