Dynamics 365 Web API 批處理(batch requests)
在組織服務中有executemultiple來執行批處理,那web api是否有批處理呢?答案是有的。
批處理的請求url很簡單[Organization URI]/api/data/v8.2/$batch,在最後加個$batch就行,但body非常的難拼,換行空格大小寫,么蛾子很多,本篇分別分享下js中的body拼寫和C#中的body拼寫,著重介紹下我在拼的過程中遇到的么蛾子。
先上js的body,這裡我放了一個post的新建和一個patch的更新
var data = [];
data.push('--batch_123456');
data.push('Content-Type: multipart/mixed;boundary=changeset_BBB456');
data.push('');
data.push('--changeset_BBB456');
data.push('Content-Type:application/http');
data.push('Content-Transfer-Encoding:binary');
data.push('Content-ID:1');
data.push('');
data.push('POST ' + Xrm.Page.context.getClientUrl() + '/api/data/v8.2/accounts HTTP/1.1');
data.push('Content-Type:application/json;type=entry');
data.push('');
data.push('{ "name": "batch acount 20"}');
data.push('--changeset_BBB456');
data.push('Content-Type:application/http');
data.push('Content-Transfer-Encoding:binary');
data.push('Content-ID:2');
data.push('');
data.push('PATCH ' + Xrm.Page.context.getClientUrl() + '/api/data/v8.2/accounts(760C2F6D-276D-E811-80DC-0017FA01322A)' + ' HTTP/1.1');
data.push('Content-Type:application/json;type=entry');
data.push('');
data.push('{ "telephone1": "13223874637"}');
data.push('--changeset_BBB456--');
data.push('--batch_123456--');
var payload = data.join('\r\n');
此處的--batch_123456,下劃線後面的123456是隨機定義的,只要前後對應即可,--changeset_BBB456,這裡的下劃線後面的BB456也是一樣隨機的,只要前後對應即可,最要注意的是空行
下面的是斷點跟蹤取到的body的內容,和SDK sample中的body體,如果拼的過程中遇到錯誤就嚴格對照sample的body格式
"--batch_123456
Content-Type: multipart/mixed;boundary=changeset_BBB456
--changeset_BBB456
Content-Type:application/http
Content-Transfer-Encoding:binary
Content-ID:1
POST http://139.219.130.107:5555/YunHai/api/data/v8.2/accounts HTTP/1.1
Content-Type:application/json;type=entry
{ "name": "batch acount 20"}
--changeset_BBB456
Content-Type:application/http
Content-Transfer-Encoding:binary
Content-ID:2
PATCH http://139.219.130.107:5555/YunHai/api/data/v8.2/accounts(760C2F6D-276D-E811-80DC-0017FA01322A) HTTP/1.1
Content-Type:application/json;type=entry
{ "telephone1": "13223874637"}
--changeset_BBB456--
--batch_123456--"
注意HTTP method要全部大寫,比如我寫成patch就不對
再附上C#中的body體,這裡注意每行body後面的\n換行符,以及該換行的地方要另加換行符\n
var body = "--batch_webapifetch\n";
body += "Content-Type:multipart/mixed;boundary=changeset_BBB456\n";
body += "\n";
body += "--changeset_BBB456\n";
body += "Content-Type:application/http\n";
body += "Content-Transfer-Encoding: binary\n";
body += "Content-ID:1\n";
body += "\n";
body += "POST http://139.219.130.107:5555/yunhai/api/data/v8.2/accounts HTTP/1.1\n";
body += "Content-Type: application/json\n";
body += "OData-Version: 4.0\n";
body += "OData-MaxVersion: 4.0\n";
body += "\n";
body += "{\"name\": \"batch acount 200\"}\n";
body += "--changeset_BBB456\n";
body += "Content-Type:application/http\n";
body += "Content-Transfer-Encoding: binary\n";
body += "Content-ID:2\n";
body += "\n";
body += "PATCH http://139.219.130.107:5555/yunhai/api/data/v8.2/accounts(760C2F6D-276D-E811-80DC-0017FA01322A) HTTP/1.1\n";
body += "Content-Type: application/json\n";
body += "OData-Version: 4.0\n";
body += "OData-MaxVersion: 4.0\n";
body += "\n";
body += "{\"telephone1\": \"18621384365\"}\n";
body += "--changeset_BBB456--\n";
body += "--batch_webapifetch--";
再重點強調下格式,比如我以為url後面的HTTP/1.1應該是個無傷大雅的東西吧就不加了,結果就是400,加上後就對了,按官方文件的說法Batch requests包含的request上限是100,然後返回的batchresponse是下面這樣的,是一個文字而非一個json物件,所以response的處理上需要你自己寫個什麼處理方法了。
"--batchresponse_0a42cdd6-f6d4-4cce-b509-ed73c74bff23
Content-Type: multipart/mixed; boundary=changesetresponse_79ef0f48-d5e1-4090-81b8-d89a32f64bdc
--changesetresponse_79ef0f48-d5e1-4090-81b8-d89a32f64bdc
Content-Type: application/http
Content-Transfer-Encoding: binary
Content-ID: 1
HTTP/1.1 204 No Content
OData-Version: 4.0
Location: http://139.219.130.107:5555/YunHai/api/data/v8.2/accounts(906fe6d6-3e6d-e811-80dc-0017fa01322a)
OData-EntityId: http://139.219.130.107:5555/YunHai/api/data/v8.2/accounts(906fe6d6-3e6d-e811-80dc-0017fa01322a)
Access-Control-Expose-Headers: Preference-Applied,OData-EntityId,Location,ETag,OData-Version,Content-Encoding,Transfer-Encoding,Content-Length,Retry-After
--changesetresponse_79ef0f48-d5e1-4090-81b8-d89a32f64bdc
Content-Type: application/http
Content-Transfer-Encoding: binary
Content-ID: 2
HTTP/1.1 204 No Content
OData-Version: 4.0
Location: http://139.219.130.107:5555/YunHai/api/data/v8.2/accounts(760C2F6D-276D-E811-80DC-0017FA01322A)
OData-EntityId: http://139.219.130.107:5555/YunHai/api/data/v8.2/accounts(760C2F6D-276D-E811-80DC-0017FA01322A)
Access-Control-Expose-Headers: Preference-Applied,OData-EntityId,Location,ETag,OData-Version,Content-Encoding,Transfer-Encoding,Content-Length,Retry-After
--changesetresponse_79ef0f48-d5e1-4090-81b8-d89a32f64bdc--
--batchresponse_0a42cdd6-f6d4-4cce-b509-ed73c74bff23--
"
相關文章
- Dynamics 365 Web API模擬使用者WebAPI
- Dynamics 365 Web API Set Values of all Data Types using Web API in Dynamics CRM Through C#WebAPIC#
- Dynamics CRM 2013 批處理
- Difference between Microsoft Dynamics 365 WEB API, Organization Service and Organization Data ServicROSWebAPI
- Dynamics 365(三)WEB資源使用Web
- Spring Boot 之 Spring Batch 批處理實踐Spring BootBAT
- Spring Batch 基本的批處理指導原則SpringBAT
- Dynamics 365 Web Api 檢查使用者是否具有某個安全形色WebAPI
- 《Spring Batch 權威指南》之“批處理和 Spring”SpringBAT
- 配置 Spring Batch 批處理失敗重試機制SpringBAT
- 一文輕鬆搞定批處理框架 Spring Batch框架SpringBAT
- Dynamics 365中的Client API form context (formContext)clientAPIORMContext
- Dynamics 365 Web Api 建立與更新返回記錄ID及更新後的資料記錄WebAPI
- Dynamics 365 基於ADFS 2016(4.0)的OAuth2 CRM Web API RequestOAuthWebAPI
- MySQL使用Batch批量處理MySqlBAT
- Windows批處理學習(二)——批處理(3)薦Windows
- Dynamics 365 Portal如何清除快取快取
- 批處理框架框架
- JDBC 批處理JDBC
- 碎片批處理
- 批處理命令
- 批處理概述
- Dynamics 365(on-premises) Portal 的安裝REM
- Dynamics 365 控制右側欄的介面
- 使用Kafka分割槽擴充套件Spring Batch大資料排程批處理 – ArnoldKafka套件SpringBAT大資料
- requests庫中的Cookie處理Cookie
- windows批處理之一:字串處理Windows字串
- Windows批處理命令Windows
- 批處理命令大全
- 批處理壓縮
- 批處理----學習
- DOS 批處理命令
- Java後端開發中的任務排程:使用Spring Batch實現批處理Java後端SpringBAT
- window 批處理檔案
- 什麼是批處理
- 批處理髮郵件
- 批處理檔案命令
- Windows批處理檔案Windows