批量请求 – Dynamics CRM

所有,

我正在尝试使用以下源代码向Dynamics CRM实施批处理请求:

public async Task HttpPatchCrmApi(string resource, string data) { string uniq = Guid.NewGuid().ToString(); MultipartContent content = new MultipartContent("mixed", "batch_" + uniq); HttpRequestMessage batchRequest = new HttpRequestMessage(HttpMethod.Post, CrmBaseUrl + "/api/data/v8.0/$batch"); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, CrmBaseUrl + resource); request.Content = new StringContent(data, Encoding.UTF8, "application/json"); HttpMessageContent query = new HttpMessageContent(request); content.Add(query); batchRequest.Content = content; HttpResponseMessage response = await RbWebApi.SendAsync(batchRequest); return response; } 

问题是我得到“ 400 Bad request

编辑:正如这里的评论中所建议的那样是来自fiddler的请求的堆栈跟踪:

 POST https://Hidden.api.crm4.dynamics.com/api/data/v8.0/$batch HTTP/1.1 Authorization: Bearer eyJ0eXAiOiJKV.... very long string Content-Type: multipart/mixed; boundary="batch_7b6e3c60-1284-4958-a39a-4653af21833c" Host: Hidden.api.crm4.dynamics.com Content-Length: 313 Expect: 100-continue --batch_7b6e3c60-1284-4958-a39a-4653af21833c Content-Type: application/http; msgtype=request POST /api/data/v8.0/my_recurringgifts HTTP/1.1 Host: Hidden.api.crm4.dynamics.com Content-Type: application/json; charset=utf-8 {"my_name":"slavi"} --batch_7b6e3c60-1284-4958-a39a-4653af21833c-- 

在编写代码的同时,我从这里和这里开始鼓舞自己

我认为你的要求是错误的。 您必须像Microsoft定义的那样构建请求Body

这意味着空行必须在正确的位置,所有属性必须存在于正文中(例如“–changeset_XXX”),因为我看到你不满足这个要求。

我只是在Postman中根据我的CRM建立一个请求,它起作用了:


url

 https://yourTenant.api.crm.dynamics.com/api/data/v8.0/$batch 

 OData-MaxVersion:4.0 OData-Version:4.0 Accept:application/json Authorization:Bearer aVeryLongStringTokenHere Content-Type: multipart/mixed;boundary=batch_1234567 

身体

 --batch_1234567 Content-Type:multipart/mixed;boundary=changeset_555666 --changeset_555666 Content-Type:application/http Content-Transfer-Encoding:binary Content-ID:1 POST https://yourTenant.api.crm.dynamics.com/api/data/v8.0/accounts HTTP/1.1 Content-Type:application/json;type=entry {name: 'BatchJobTest788'} --changeset_555666 Content-Type:application/http Content-Transfer-Encoding:binary Content-ID:2 POST https://yourTenant.api.crm.dynamics.com/api/data/v8.0/accounts HTTP/1.1 Content-Type:application/json;type=entry {new_name: 'BatchJobTest348'} --changeset_555666-- --batch_1234567-- 

补充说明:

  • 您的标题的Content-Type包含您的BatchId
  • 您的批次的内容类型包含您的ChangesetId(如果它是对数据的更改)
  • 在开始编程之前,REST调用尝试在像POSTMAN这样的REST工具中定义它们并使它们工作。 然后在代码中构建工作请求。
  • 这里有一个很好的解释 – CRM中的批处理来源