All,
我正在尝试使用以下源代码对 Dynamics CRM 实施批量请求:
public async Task<HttpResponseMessage> 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 错误请求"
编辑:
正如评论中所建议的,这里是来自 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--
在编写代码时我受到启发here http://bartwullems.blogspot.dk/2015/01/aspnet-web-api-request-batching.html and here https://stackoverflow.com/questions/21734003/applying-default-headers-to-multi-part-request-content
我认为你的要求是错误的。
您必须构建请求正文EXACTLY like 由微软定义 https://msdn.microsoft.com/en-us/library/mt607719.aspx
这意味着空白行必须位于正确的位置,所有属性必须存在于正文中(例如“--changeset_XXX”),并且我发现您不满足此要求。
我只是在 Postman 中针对我的 CRM 构建了一个请求,它起作用了:
URL
https://yourTenant.api.crm.dynamics.com/api/data/v8.0/$batch
Headers
OData-MaxVersion:4.0
OData-Version:4.0
Accept:application/json
Authorization:Bearer aVeryLongStringTokenHere
Content-Type: multipart/mixed;boundary=batch_1234567
Body
--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
- Batch 的 Content-Type 保存您的 ChangesetId(如果是数据更改)
- 在开始对 REST 调用进行编程之前,请尝试在 POSTMAN 等 REST 工具中定义它们并使其工作。然后在您的代码中构建工作请求。
-
Here http://www.odata.org/documentation/odata-version-3-0/batch-processing/CRM 中批处理的一个很好的解释来源
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)