【问题标题】:Batch request - Dynamics CRM批量请求 - Dynamics CRM
【发布时间】:2016-04-20 17:25:57
【问题描述】:

全部,

我正在尝试使用以下源代码对 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 Bad request

编辑: 正如 cmets 中所建议的,这里是来自提琴手的请求的堆栈跟踪:

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

在编写代码时,我受到herehere 的启发

【问题讨论】:

标签: c# asp.net-web-api dynamics-crm


【解决方案1】:

我认为你的要求是错误的。 您必须像defined by Microsoft

一样构建请求正文完全

这意味着空白行必须在正确的位置,所有属性都必须存在于正文中(例如“--changeset_XXX”),我认为您不符合此要求。

我刚刚在 Postman 中针对我的 CRM 构建了一个请求,它起作用了:


网址

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

补充说明:

  • 您的 Header 的 Content-Type 包含您的 BatchId
  • 您的 Batch 的 Content-Type 包含您的 ChangesetId(如果是对数据的更改)
  • 在开始对 REST 调用进行编程之前,请尝试在 POSTMAN 等 REST 工具中定义它们并使其工作。然后在您的代码中构建工作请求。
  • Here CRM 中批处理的一个很好的解释来源

【讨论】:

    猜你喜欢
    • 2017-01-23
    • 2012-01-21
    • 1970-01-01
    • 1970-01-01
    • 2013-10-20
    • 1970-01-01
    • 2017-06-06
    • 2014-03-29
    • 1970-01-01
    相关资源
    最近更新 更多