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 Set Values of all Data Types using Web API in Dynamics CRM Through C#WebAPIC#
- Dynamics 365 Web API模擬使用者WebAPI
- 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
- Dynamics 365 基於ADFS 2016(4.0)的OAuth2 CRM Web API RequestOAuthWebAPI
- Dynamics 365中的Client API form context (formContext)clientAPIORMContext
- Spring Batch 基本的批處理指導原則SpringBAT
- 《Spring Batch 權威指南》之“批處理和 Spring”SpringBAT
- Dynamics 365 Web Api 檢查使用者是否具有某個安全形色WebAPI
- 一文輕鬆搞定批處理框架 Spring Batch框架SpringBAT
- 配置 Spring Batch 批處理失敗重試機制SpringBAT
- 使用Kafka分割槽擴充套件Spring Batch大資料排程批處理 – ArnoldKafka套件SpringBAT大資料
- requests庫中的Cookie處理Cookie
- Dynamics 365 控制右側欄的介面
- 批處理概述
- 如何將Dynamics CRM 2016(on-premises)升級到Dynamics 365(on-premises)REM
- Spring Batch + JPA 處理 Excel 檔案教程SpringBATExcel
- Java後端開發中的任務排程:使用Spring Batch實現批處理Java後端SpringBAT
- Spring中實現面向寫入的批次和批處理APISpringAPI
- VSCode JavaScript snippets for Dynamics 365/CRM 2011/2013/2015/2016VSCodeJavaScript
- Apache Beam,批處理和流式處理的融合!Apache
- 什麼是批處理
- bat 批處理字串操作BAT字串
- window 批處理檔案
- 資料共享-spring batch(9)上下文處理SpringBAT
- Spring Batch:將資料從Web服務處理到MongoDBSpringBATWebMongoDB
- web api 返回Json資料中人格式帶T處理WebAPIJSON
- Python requests-html扔多處理錯誤PythonHTML
- 簡單實現批處理
- Python批處理:檔案操作Python
- MPP架構和批處理架構
- bat批處理常用指令碼BAT指令碼
- java當中的批處理Java
- JDBC當中的批處理JDBC
- 批處理命令之tree命令
- Go Web如何處理Web請求?GoWeb