SAP集成中ODATA $batch请求传递ETag的实现疑问
SAP OData $batch请求中ETag跨请求传递解决方案
核心问题说明
OData的$batch请求中,同一个**changeset(变更集)**内的请求是原子性执行的——要么全部成功,要么全部回滚。在此过程中,客户端无法获取中间请求的响应头(包括修改实体后生成的新ETag),因此无法在同一个changeset内的请求间动态传递ETag。
可行解决方案
将依赖ETag的操作拆分为多轮独立的batch请求序列,每轮完成后从响应中提取最新ETag,用于下一轮的修改请求。具体步骤如下:
- 第一轮:获取初始ETag并更新出库交付行项目
Content-Type: multipart/mixed; boundary=batch_01 --batch_01 Content-Type: application/http Content-Transfer-Encoding: binary GET A_OutbDeliveryHeader(DeliveryDocument='0000000001') HTTP/1.1 sap-context-accept: header Content-Type: application/json Accept: application/json Content-ID: C1 --batch_01 Content-Type: multipart/mixed; boundary=changeset_01 --changeset_01 Content-Type: application/http Content-Transfer-Encoding: binary PATCH A_OutbDeliveryItem(DeliveryDocument='0000000001',DeliveryDocumentItem='10') HTTP/1.1 sap-context-accept: header Content-Type: application/json Accept: application/json If-Match: <从C1响应头中提取的初始ETag> { "ActualDeliveryQuantity": "2.000" } --changeset_01-- --batch_01--
- 第二轮:获取更新后的表头ETag,修改表头并执行过账
Content-Type: multipart/mixed; boundary=batch_02 --batch_02 Content-Type: application/http Content-Transfer-Encoding: binary GET A_OutbDeliveryHeader(DeliveryDocument='0000000001') HTTP/1.1 sap-context-accept: header Content-Type: application/json Accept: application/json Content-ID: C3 --batch_02 Content-Type: multipart/mixed; boundary=changeset_02 --changeset_02 Content-Type: application/http Content-Transfer-Encoding: binary PATCH A_OutbDeliveryHeader(DeliveryDocument='0000000001') HTTP/1.1 sap-context-accept: header Content-Type: application/json Accept: application/json If-Match: <从C3响应头中提取的最新ETag> { "ActualGoodsMovementDate": "2023-03-14T13:33:10", "ActualGoodsMovementTime": "PT13H33M10S" } --changeset_02-- --batch_02 Content-Type: application/http Content-Transfer-Encoding: binary GET A_OutbDeliveryHeader(DeliveryDocument='0000000001') HTTP/1.1 sap-context-accept: header Content-Type: application/json Accept: application/json Content-ID: C4 --batch_02 Content-Type: multipart/mixed; boundary=changeset_03 --changeset_03 Content-Type: application/http Content-Transfer-Encoding: binary POST PostGoodsIssue?DeliveryDocument='0000000001' HTTP/1.1 sap-context-accept: header Content-Type: application/json Accept: application/json If-Match: <从C4响应头中提取的最新ETag> --changeset_03-- --batch_02--
关键注意事项
- 每轮batch执行完成后,务必从对应GET请求的响应头中提取
ETag字段,替换下一轮请求的If-Match值。 - 禁止将依赖动态ETag的修改请求放在同一个changeset内,否则无法获取中间步骤生成的新ETag。
- 无依赖关系的请求可合并到同一个batch的独立段中,减少请求次数。
内容的提问来源于stack exchange,提问作者Jijo Joseph
相关产品推荐
相关产品推荐

