Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

365 online webapi batch command response badrequest

Posted on by Microsoft Employee

my content as below and the request return badrequest status:

POST https://xxxx.crm7.dynamics.com/api/data/v8.0/$batch HTTP/1.1
OData-MaxVersion: 4.0
OData-Version: 4.0
Accept: application/json
Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6IjlGWERwYmZNRlQyU3ZRdVhoODQ2WVR3RUlCdyIsImtpZCI6IjlGWERwYmZNRlQyU3ZRdVhoODQ2WVR3RUlCdyJ9.eyJhdWQiOiJodHRwczovL3B0YmkuY3JtNy5keW5hbWljcy5jb20vIiwiaXNzIjoiaHR0cHM6Ly9zdHMud2luZG93cy5uZXQvY2ExNDczYmUtMWVlYS00NTU5LWIwYmEtZWE4NThlOTI5YmRmLyIsImlhdCI6MTQ5ODIwNzIyMiwibmJmIjoxNDk4MjA3MjIyLCJleHAiOjE0OTgyMTExMjIsImFjciI6IjEiLCJhaW8iOiJZMlpnWUdnelhpNldIN2o4ZTJCZTNBOE9mN2xsb3BkRG1BNVoyYTgvbERiSlUwK1Q2UzBBIiwiYW1yIjpbInB3ZCJdLCJhcHBpZCI6IjBkN2Q4NzFlLWY2ZjEtNDZiMi1iZjY0LThiM2YxYTUwODFlZSIsImFwcGlkYWNyIjoiMCIsImVfZXhwIjoyNjI4MDAsImZhbWlseV9uYW1lIjoi44K944OzIiwiZ2l2ZW5fbmFtZSI6IuOCqyIsImlwYWRkciI6IjEyNC43NC4xMDguMTU4IiwibmFtZSI6IuWtqyDkvbMiLCJvaWQiOiIwMWFiNTM0OC01ZTI3LTRiYzctYTA2YS04NzA0NGQ1NTlkODEiLCJvbnByZW1fc2lkIjoiUy0xLTUtMjEtMTk2NDMyMjU4Mi0yOTQyOTA1My05Njg5MTIwMzctMjAyOTEyIiwicGxhdGYiOiIzIiwicHVpZCI6IjEwMDMwMDAwQTE4RUQxQzAiLCJzY3AiOiJ1c2VyX2ltcGVyc29uYXRpb24iLCJzdWIiOiIyZFN0cVhWN2FaWFBjU1pyQUJNUi1pai1zWDlxeE56aDVrRmNIYU45dE9vIiwidGlkIjoiY2ExNDczYmUtMWVlYS00NTU5LWIwYmEtZWE4NThlOTI5YmRmIiwidW5pcXVlX25hbWUiOiJwdHowMDAwMDVAZ2ludHJhLWV4LmJjLWdsZXgubmV0IiwidXBuIjoicHR6MDAwMDA1QGdpbnRyYS1leC5iYy1nbGV4Lm5ldCIsInZlciI6IjEuMCJ9.GD6VFY6mp-rvrx4hjO22oo0m0rLtMn_1zmyAKKoiIepQn5aJYf_MTU6Qn8ZJ-sf19oJuC7y16csQyksElsr39TaQRYZJiyNZXxZB-sMREOOuDlKptUU-2I3CGguJ7_0VmClP4aVaIlnWrgDLR2j-5F00utD80lItnyM4Ao7xlyup49b6un1T9sOuH7b4zuApPonJIjM38PXw-Yw3a1L6C901FVGqaDQT5MzjOkBTj_6_q0fiMYKICOuexLVd_5EYghMadP5M2N6dYBURk2jPhs6g23UzeegdqvDBeXYllDO0bSEpxCNA1galtFj9LW2Sv1-XBqSKlYK4Hb5bquV8WQ
Content-Type: multipart/mixed; boundary="batch_32d0d46d-8cb5-4777-bfdf-cc1476579f3f"
Host: ptbi.crm7.dynamics.com
Content-Length: 1062
Expect: 100-continue

--batch_32d0d46d-8cb5-4777-bfdf-cc1476579f3f
Content-Type: multipart/mixed; boundary="changeset_1614cd2e-76af-45b7-8d9b-0ee0f3263720"

--changeset_1614cd2e-76af-45b7-8d9b-0ee0f3263720
Content-Type: application/http; msgtype=request
Content-Transfer-Encoding: binary
Content-ID: 1

PATCH /api/data/v8.0/new_onetimepasses(new_name='FxUD6q6y',new_onetimepassnumber='36017000059') HTTP/1.1
Host: xxxx.crm7.dynamics.com
Content-Type: application/json; charset=utf-8

{
  "new_name": "FxUD6q6y",
  "new_onetimepassnumber": "36017000059"
}
--changeset_1614cd2e-76af-45b7-8d9b-0ee0f3263720
Content-Type: application/http; msgtype=request
Content-Transfer-Encoding: binary
Content-ID: 2

PATCH /api/data/v8.0/new_onetimepasses(new_name='HUQI1y4u',new_onetimepassnumber='36017000051') HTTP/1.1
Host: xxxx.crm7.dynamics.com
Content-Type: application/json; charset=utf-8

{
  "new_name": "HUQI1y4u",
  "new_onetimepassnumber": "36017000051"
}
--changeset_1614cd2e-76af-45b7-8d9b-0ee0f3263720--

--batch_32d0d46d-8cb5-4777-bfdf-cc1476579f3f--

what is the key error in this content?  did microsoft provide c# demo regarding to the webapi batch?

My code:

            var onetimepassUpdate1 = new JObject();

            onetimepassUpdate1.Add("new_name", "FxUD6q6y");

            onetimepassUpdate1.Add("new_onetimepassnumber", "36017000059");

 

            var onetimepassUpdate2 = new JObject();

            onetimepassUpdate2.Add("new_name", "HUQI1y4u");

            onetimepassUpdate2.Add("new_onetimepassnumber", "36017000051");

 

            var request1 = new HttpRequestMessage(new HttpMethod("PATCH"), "https://xxxx.crm7.dynamics.com/api/data/" + getVersionedWebAPIPath() + "new_onetimepasses(new_name='FxUD6q6y',new_onetimepassnumber='36017000059')");

            request1.Content = new StringContent(onetimepassUpdate1.ToString(), Encoding.UTF8, "application/json");

 

            var request2 = new HttpRequestMessage(new HttpMethod("PATCH"), "https://xxxx.crm7.dynamics.com/api/data/" + getVersionedWebAPIPath() + "new_onetimepasses(new_name='HUQI1y4u',new_onetimepassnumber='36017000051')");

            request2.Content = new StringContent(onetimepassUpdate2.ToString(), Encoding.UTF8, "application/json");

 

            var batchRequest = new HttpRequestMessage(new HttpMethod("POST"), getVersionedWebAPIPath() + "$batch");

            var content_batch = new MultipartContent("mixed", "batch_" + Guid.NewGuid().ToString());

 

            var content_changeset = new MultipartContent("mixed", "changeset_" + Guid.NewGuid().ToString());

            var mc1 = new HttpMessageContent(request1);

            var mc2 = new HttpMessageContent(request2);

            mc1.Headers.Add("Content-Transfer-Encoding", "binary");

            mc1.Headers.Add("Content-ID", "1");

            mc2.Headers.Add("Content-Transfer-Encoding", "binary");

            mc2.Headers.Add("Content-ID", "2");

            content_changeset.Add(mc1);

            content_changeset.Add(mc2);

 

            content_batch.Add(content_changeset);

            batchRequest.Content = content_batch;

            //var xs = await content_batch.ReadAsStringAsync();

            var batchResponse = await httpClient.SendAsync(batchRequest);

            var x = batchResponse.StatusCode;

            Console.Write(x.ToString());

*This post is locked for comments

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,240 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans