【问题标题】:Cannot create batch work items using batch operations in TFS无法使用 TFS 中的批处理操作创建批处理工作项
【发布时间】:2017-08-11 23:02:31
【问题描述】:

我在尝试使用批量创建方法创建工作项时遇到错误

错误 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”
  • “正文:x:”
  • “正文:JSON.stringify(x)”
  • “json:[body:x]”

我已经尝试将“application/json-patch+json”和“application/json”(推荐为 MIcrosoft 文档)作为 Content-Types

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

没有可用于此错误的参考资料,因此我在这一点上很糟糕。这里可能有什么问题,请帮忙..

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

【问题讨论】:

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


    【解决方案1】:

    您需要使用 "application/json" 作为 Content-Types 和 post 方法,就像描述的 tutorial of Microsoft documentation 一样。

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

    1. json:如果要直接发送json(content-type设置为 应用程序/json)
    2. files:要上传的文件对象(content-type 设置为 多部分/表单数据;边界=xxx)
    3. body:您要发送的自定义正文内容。如果使用,以前的 选项将被忽略,您的自定义正文将被发送。 (不会设置内容类型)

    Source Link

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

    【讨论】:

    • 嗨@Patrick-MSFT,我已经在方法之外生成了 json 文档并将其发送到这里,现在我收到此错误 {"count":1,"value":[{"code":400, "headers":{"Content-Type":"application/json; charset=utf-8"},"body":"{\"count\":1,\"value\":{\"Message\" :\"TF51635:更新 XML 中有重复的临时 ID。\\r\\n参数名称:workItemUpdates\"}}"}]}。这是什么原因?
    • @Lalindu 在这个问题stackoverflow.com/questions/42945345/… 中添加我的回复。如果没有 ID 信息,我在邮递员中使用这个 rest api 也会出错。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-13
    • 2018-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多