简体   繁体   中英

Batch request - Dynamics CRM

All,

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

The problem is that I am getting " 400 Bad request "

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

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

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.

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

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

Additional Remarks:

  • 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM