Batch request - Dynamics CRM
Asked Answered
A

1

1

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

Airship answered 20/4, 2016 at 17:25 Comment(2)
I would use Fiddler to make sure the requests that you send look like the examples from the SDK: msdn.microsoft.com/en-us/library/mt607719.aspxNephrectomy
Hi Polsh, updated :) I been looking at it all along but can't see difference/reason for bad request :(Airship
R
5

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
Richmound answered 21/7, 2016 at 9:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.