0

I am getting below errors while trying to create workitems with batch creation method

Error 1

"Message":"No MediaTypeFormatter is available to read an object of type 'JsonBatchHttpRequest' from content with media type 'application/json-patch+json'."

Error 2

{"count":1,"value":{"Message":"One or more errors occurred."}}

I have referred to this documentation https://www.visualstudio.com/en-us/docs/integrate/api/wit/batch from Microsoft . and my question on stackoverflaw Create Large Amount of Work Items in TFS Using Javascript REST API

I have tried to send data as below methods

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

I have tried both "application/json-patch+json" and "application/json"(recommended as MIcrosoft documentation) as Content-Types

I have tired both Post (recommended as MIcrosoft documentation) and Patch methods

There is no references available for this error hence I have sucked at this point.What could be possibly wrong here please help..

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

1 回答 1

1

您需要使用 "application/json"as Content-Types 和post方法,就像Microsoft 文档中描述的教程一样。

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

  1. json:如果要直接发送json(content-type设置为application/json)
  2. files:要上传的文件对象(content-type 设置为 multipart/form-data;boundary=xxx)
  3. 正文:您要发送的自定义正文内容。如果使用,以前的选项将被忽略,您的自定义正文将被发送。(不会设置内容类型)

来源链接

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

于 2017-03-21T09:02:08.997 回答