批处理请求-Dynamics CRM [英] Batch request - Dynamics CRM

查看:125
本文介绍了批处理请求-Dynamics CRM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部

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

I am trying to implement a batch request to Dynamics CRM with the following source code:

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错误的请求

编辑:
如评论中的建议,此处是来自提琴手的请求的堆栈跟踪:

As suggested in the comments here is the stack trace of the request from 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--

在编写代码时,我从此处此处

While writing the code I was inspiring myself from here and here

推荐答案

我认为您的请求有误。
您必须像那样构建请求正文完全由Microsoft定义

I think your request is wrong. You must build the request Body EXACTLY like defined by Microsoft

这意味着空白行必须位于正确的位置,所有属性都必须存在于主体中(例如 --changeset_XXX例如,并且我认为您不满足此要求。

This means the Blank lines must be there at the right place all the attributes must exist in the body (like "--changeset_XXX" for example) and as I see you dont meet this requirements.

我只是在邮递员中针对我的CRM建立了一个Request,它起作用了:

I just build a Request in Postman against my CRM and it worked:

URL

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



< hr>

标题

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






附加说明:


  • 标题的内容类型保存您的BatchId

  • 批次的内容类型保存您的ChangesetId(如果它是更改为数据)

  • 开始编程REST之前调用尝试在像POSTMAN这样的REST工具中定义它们并使它们工作。然后在您的代码中构建工作请求。

  • 此处一个很好的解释说明,用于CRM中的批处理

  • The Content-Type of your Header holds your BatchId
  • The Content-Type of your Batch holds your ChangesetId (if it is a change to data)
  • Before starting to programm REST calls try to define them in a REST tool like POSTMAN and make them work. Then build the working request in your code.
  • Here a good explanation-source for the batching in CRM

这篇关于批处理请求-Dynamics CRM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆