Dynamics 365 Web API 批處理(batch requests)

vic0228發表於2018-06-11

   在組織服務中有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--
"

相關文章