Azure Table: Atomically Merge and Delete Entities
See more Azure Table Service Examples
Reads two entities and captures their exact ETags, then submits a conditional MERGE for one and DELETE for the other. Concurrent changes fail the whole changeset. Disabled by default. Uses exactly two entities with one fixed table/partition, unique Content-IDs, CRLF multipart framing, and a byte-count check against the 4 MiB request limit. Dynamic batches must additionally preserve the 100-operation, single-partition and unique-entity constraints.
Parses nested MIME responses using Chilkat.Mime, validates inner HTTP status and Content-ID uniqueness/completeness, and requires 204 for both operations. Outer 202 alone is not success. A lost or malformed response has an uncertain outcome; inspect state before replaying. Separate batches are not atomic together.
Targets Azure Storage Table service in the public cloud, not Azure Cosmos DB for Table. Uses Chilkat 11.6.0 or later and an unlocked library. Direct data requests pin x-ms-version 2026-04-06 and OData 3.0. Shared Key examples read private qa_data/azure/storage.json with account and access_key string fields. Authentication examples explain their additional fields. Create private local directories before running; keep credentials, tokens, SAS, entity values, checkpoints and diagnostics outside public content and source control.
HTTPS certificates are verified. Transport completion and HTTP status are checked separately. Writes are not automatically retried after an uncertain outcome. Entity keys, ETags and continuation tokens remain strings. Examples operate on the existing ExampleCustomers table unless noted; review the targets before adapting them. Microsoft Entra ID with suitable Table data roles is preferred where available.
Chilkat AutoIt Downloads
Local $bSuccess = False
; StringBuilder compares text by value in every generated language.
$oTextCompare = ObjCreate("Chilkat.StringBuilder")
Local $bApplyChange = False
If ($bApplyChange <> True) Then
ConsoleWrite("This example writes an atomic transaction. Review the target and deliberately enable applyChange." & @CRLF)
Exit
EndIf
; Assumes Chilkat is unlocked. See Global Unlock Sample.
$bSuccess = False
$oConfig = ObjCreate("Chilkat.JsonObject")
$oConfig.Strict = True
$bSuccess = $oConfig.LoadFile("qa_data/azure/storage.json")
If ($bSuccess <> True) Then
ConsoleWrite("Could not load private Azure configuration." & @CRLF)
Exit
EndIf
$oTextCompare.Clear
$oTextCompare.SetString($oConfig.StringOf("account"))
Local $bSameText1 = $oTextCompare.ContentsEqual("",True)
If (($oConfig.JsonTypeOf("account") <> 1) Or ($bSameText1 = True)) Then
ConsoleWrite("Supply the storage account name." & @CRLF)
Exit
EndIf
; access_key is the base64 account key, not a connection string or SAS.
$oTextCompare.Clear
$oTextCompare.SetString($oConfig.StringOf("access_key"))
Local $bSameText2 = $oTextCompare.ContentsEqual("",True)
If (($oConfig.JsonTypeOf("access_key") <> 1) Or ($bSameText2 = True)) Then
ConsoleWrite("Supply the base64 account access key." & @CRLF)
Exit
EndIf
$oAccountText = ObjCreate("Chilkat.StringBuilder")
$bSuccess = $oAccountText.Append($oConfig.StringOf("account"))
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
If (($oAccountText.Length < 3) Or ($oAccountText.Length > 24)) Then
ConsoleWrite("Storage account names need 3-24 lowercase letters or digits." & @CRLF)
Exit
EndIf
$oAllowedAccountChars = ObjCreate("Chilkat.StringBuilder")
$bSuccess = $oAllowedAccountChars.Append("abcdefghijklmnopqrstuvwxyz0123456789")
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
Local $iCharIndex = 0
While ($iCharIndex < $oAccountText.Length)
If ($oAllowedAccountChars.Contains($oAccountText.GetRange($iCharIndex,1,False),True) <> True) Then
ConsoleWrite("Storage account names need lowercase letters or digits." & @CRLF)
Exit
EndIf
$iCharIndex = $iCharIndex + 1
Wend
$oEndpoint = ObjCreate("Chilkat.StringBuilder")
$bSuccess = $oEndpoint.Append($oConfig.StringOf("account"))
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
$bSuccess = $oEndpoint.Append(".table.core.windows.net")
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
; Table names contain only letters and digits and are case-insensitive.
; Entity PartitionKey and RowKey values are case-sensitive.
Local $sResourcePath = "/$batch"
$oRest = ObjCreate("Chilkat.Rest")
$oRest.ConnectTimeoutMs = 20000
$oRest.IdleTimeoutMs = 60000
$oAzAuth = ObjCreate("Chilkat.AuthAzureStorage")
$oAzAuth.Account = $oConfig.StringOf("account")
$oAzAuth.AccessKey = $oConfig.StringOf("access_key")
$oAzAuth.Service = "Table"
$oAzAuth.Scheme = "SharedKey"
$oAzAuth.XMsVersion = "2026-04-06"
$bSuccess = $oRest.SetAuthAzureStorage($oAzAuth)
If ($bSuccess <> True) Then
ConsoleWrite("Could not configure Shared Key signing." & @CRLF)
Exit
EndIf
$oConnection = ObjCreate("Chilkat.Socket")
$oConnection.RequireSslCertVerify = True
$oConnection.MaxReadIdleMs = 60000
$oConnection.MaxSendIdleMs = 60000
$bSuccess = $oConnection.Connect($oEndpoint.GetAsString(),443,True,20000)
If ($bSuccess <> True) Then
ConsoleWrite("TLS connection failed; inspect diagnostics privately." & @CRLF)
Exit
EndIf
; Do not replay a write automatically after losing its response.
$bSuccess = $oRest.UseConnection($oConnection,False)
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
Local $sResponseText
$oResult = ObjCreate("Chilkat.JsonObject")
$oResult.Strict = True
$bSuccess = $oRest.AddHeader("DataServiceVersion","3.0;NetFx")
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
$bSuccess = $oRest.AddHeader("MaxDataServiceVersion","3.0;NetFx")
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
$bSuccess = $oRest.AddHeader("Content-Type","application/json")
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
$bSuccess = $oRest.AddHeader("Accept","application/json;odata=minimalmetadata")
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
$sResourcePath = "/ExampleCustomers(PartitionKey='customers-west',RowKey='customer-001')"
$sResponseText = $oRest.FullRequestNoBody("GET",$sResourcePath)
If ($oRest.LastMethodSuccess <> True) Then
ConsoleWrite("Request did not complete; inspect the target before retrying a write." & @CRLF)
Exit
EndIf
If ($oRest.ResponseStatusCode <> 200) Then
ConsoleWrite("HTTP status: " & $oRest.ResponseStatusCode & @CRLF)
ConsoleWrite("Request ID: " & $oRest.ResponseHdrByName("x-ms-request-id") & @CRLF)
; 404 is missing, 409 is conflict, and 412 is a stale ETag: none are success.
; Keep response bodies private. Reconcile uncertain writes before retrying.
Exit
EndIf
$bSuccess = $oResult.Load($sResponseText)
If ($bSuccess <> True) Then
ConsoleWrite("Expected well-formed JSON." & @CRLF)
Exit
EndIf
$oTextCompare.Clear
$oTextCompare.SetString($oResult.StringOf("PartitionKey"))
Local $bSameText3 = $oTextCompare.ContentsEqual("customers-west",True)
$oTextCompare.Clear
$oTextCompare.SetString($oResult.StringOf("RowKey"))
Local $bSameText4 = $oTextCompare.ContentsEqual("customer-001",True)
If (($bSameText3 <> True) Or ($bSameText4 <> True)) Then
ConsoleWrite("Unexpected entity in batch preflight." & @CRLF)
Exit
EndIf
Local $sEtag1 = $oRest.ResponseHdrByName("ETag")
$oTextCompare.Clear
$oTextCompare.SetString($sEtag1)
Local $bSameText5 = $oTextCompare.ContentsEqual("",True)
$oTextCompare.Clear
$oTextCompare.SetString($sEtag1)
Local $bSameText6 = $oTextCompare.ContentsEqual("*",True)
If (($bSameText5 = True) Or ($bSameText6 = True)) Then
ConsoleWrite("Batch needs exact entity ETags." & @CRLF)
Exit
EndIf
$oRest.Disconnect(10)
$bSuccess = $oConnection.Connect($oEndpoint.GetAsString(),443,True,20000)
If ($bSuccess <> True) Then
ConsoleWrite("TLS reconnection failed." & @CRLF)
Exit
EndIf
$bSuccess = $oRest.UseConnection($oConnection,False)
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
$sResourcePath = "/ExampleCustomers(PartitionKey='customers-west',RowKey='customer-002')"
$sResponseText = $oRest.FullRequestNoBody("GET",$sResourcePath)
If ($oRest.LastMethodSuccess <> True) Then
ConsoleWrite("Request did not complete; inspect the target before retrying a write." & @CRLF)
Exit
EndIf
If ($oRest.ResponseStatusCode <> 200) Then
ConsoleWrite("HTTP status: " & $oRest.ResponseStatusCode & @CRLF)
ConsoleWrite("Request ID: " & $oRest.ResponseHdrByName("x-ms-request-id") & @CRLF)
; 404 is missing, 409 is conflict, and 412 is a stale ETag: none are success.
; Keep response bodies private. Reconcile uncertain writes before retrying.
Exit
EndIf
$bSuccess = $oResult.Load($sResponseText)
If ($bSuccess <> True) Then
ConsoleWrite("Expected well-formed JSON." & @CRLF)
Exit
EndIf
$oTextCompare.Clear
$oTextCompare.SetString($oResult.StringOf("PartitionKey"))
Local $bSameText7 = $oTextCompare.ContentsEqual("customers-west",True)
$oTextCompare.Clear
$oTextCompare.SetString($oResult.StringOf("RowKey"))
Local $bSameText8 = $oTextCompare.ContentsEqual("customer-002",True)
If (($bSameText7 <> True) Or ($bSameText8 <> True)) Then
ConsoleWrite("Unexpected entity in batch preflight." & @CRLF)
Exit
EndIf
Local $sEtag2 = $oRest.ResponseHdrByName("ETag")
$oTextCompare.Clear
$oTextCompare.SetString($sEtag2)
Local $bSameText9 = $oTextCompare.ContentsEqual("",True)
$oTextCompare.Clear
$oTextCompare.SetString($sEtag2)
Local $bSameText10 = $oTextCompare.ContentsEqual("*",True)
If (($bSameText9 = True) Or ($bSameText10 = True)) Then
ConsoleWrite("Batch needs exact entity ETags." & @CRLF)
Exit
EndIf
$oRest.Disconnect(10)
$bSuccess = $oConnection.Connect($oEndpoint.GetAsString(),443,True,20000)
If ($bSuccess <> True) Then
ConsoleWrite("TLS reconnection failed." & @CRLF)
Exit
EndIf
$bSuccess = $oRest.UseConnection($oConnection,False)
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
$sResourcePath = "/$batch"
; Exactly two distinct entities in the same table and partition, by construction.
; Dynamic plans must also enforce <=100 operations, one operation per entity, and one partition.
$oRandom = ObjCreate("Chilkat.Prng")
Local $sNonce = $oRandom.GenRandom(16,"hex")
If ($oRandom.LastMethodSuccess <> True) Then
ConsoleWrite("Could not create MIME boundaries." & @CRLF)
Exit
EndIf
$oBatchBoundary = ObjCreate("Chilkat.StringBuilder")
$oChangesetBoundary = ObjCreate("Chilkat.StringBuilder")
$bSuccess = $oBatchBoundary.Append("batch_")
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
$bSuccess = $oBatchBoundary.Append($sNonce)
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
$bSuccess = $oChangesetBoundary.Append("changeset_")
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
$bSuccess = $oChangesetBoundary.Append($sNonce)
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
$oWire = ObjCreate("Chilkat.StringBuilder")
$bSuccess = $oWire.Append("--")
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
$bSuccess = $oWire.Append($oBatchBoundary.GetAsString())
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
$bSuccess = $oWire.Append(@CRLF & "Content-Type: multipart/mixed; boundary=")
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
$bSuccess = $oWire.Append($oChangesetBoundary.GetAsString())
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
$bSuccess = $oWire.Append(@CRLF & @CRLF)
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
$bSuccess = $oWire.Append("--")
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
$bSuccess = $oWire.Append($oChangesetBoundary.GetAsString())
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
$bSuccess = $oWire.Append(@CRLF & "Content-Type: application/http" & @CRLF & "Content-Transfer-Encoding: binary" & @CRLF & "Content-ID: 1" & @CRLF & @CRLF & "MERGE https://")
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
$bSuccess = $oWire.Append($oEndpoint.GetAsString())
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
$bSuccess = $oWire.Append("/ExampleCustomers(PartitionKey='customers-west',RowKey='customer-001') HTTP/1.1" & @CRLF & "Accept: application/json;odata=minimalmetadata" & @CRLF & "DataServiceVersion: 3.0;NetFx" & @CRLF & "MaxDataServiceVersion: 3.0;NetFx" & @CRLF)
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
$bSuccess = $oWire.Append("If-Match: ")
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
$bSuccess = $oWire.Append($sEtag1)
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
$bSuccess = $oWire.Append(@CRLF)
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
$oItem1 = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oItem1.UpdateString("PartitionKey","customers-west")
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
$bSuccess = $oItem1.UpdateString("RowKey","customer-001")
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
$bSuccess = $oItem1.UpdateString("Address","Batch address 1")
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
$bSuccess = $oWire.Append("Content-Type: application/json" & @CRLF & "Prefer: return-no-content" & @CRLF & @CRLF)
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
$bSuccess = $oWire.Append($oItem1.Emit())
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
$bSuccess = $oWire.Append(@CRLF)
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
$bSuccess = $oWire.Append("--")
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
$bSuccess = $oWire.Append($oChangesetBoundary.GetAsString())
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
$bSuccess = $oWire.Append(@CRLF & "Content-Type: application/http" & @CRLF & "Content-Transfer-Encoding: binary" & @CRLF & "Content-ID: 2" & @CRLF & @CRLF & "DELETE https://")
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
$bSuccess = $oWire.Append($oEndpoint.GetAsString())
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
$bSuccess = $oWire.Append("/ExampleCustomers(PartitionKey='customers-west',RowKey='customer-002') HTTP/1.1" & @CRLF & "Accept: application/json;odata=minimalmetadata" & @CRLF & "DataServiceVersion: 3.0;NetFx" & @CRLF & "MaxDataServiceVersion: 3.0;NetFx" & @CRLF)
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
$bSuccess = $oWire.Append("If-Match: ")
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
$bSuccess = $oWire.Append($sEtag2)
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
$bSuccess = $oWire.Append(@CRLF)
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
$bSuccess = $oWire.Append(@CRLF)
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
$bSuccess = $oWire.Append("--")
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
$bSuccess = $oWire.Append($oChangesetBoundary.GetAsString())
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
$bSuccess = $oWire.Append("--" & @CRLF)
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
$bSuccess = $oWire.Append("--")
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
$bSuccess = $oWire.Append($oBatchBoundary.GetAsString())
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
$bSuccess = $oWire.Append("--" & @CRLF)
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
$oPayloadBytes = ObjCreate("Chilkat.BinData")
$bSuccess = $oPayloadBytes.AppendString($oWire.GetAsString(),"utf-8")
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
If ($oPayloadBytes.NumBytes > 4194304) Then
ConsoleWrite("Batch exceeds the 4 MiB limit including multipart overhead." & @CRLF)
Exit
EndIf
$oContentType = ObjCreate("Chilkat.StringBuilder")
$bSuccess = $oContentType.Append("multipart/mixed; boundary=")
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
$bSuccess = $oContentType.Append($oBatchBoundary.GetAsString())
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
$bSuccess = $oRest.AddHeader("Content-Type",$oContentType.GetAsString())
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
$bSuccess = $oRest.AddHeader("Accept","multipart/mixed")
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
$sResponseText = $oRest.FullRequestString("POST",$sResourcePath,$oWire.GetAsString())
If ($oRest.LastMethodSuccess <> True) Then
ConsoleWrite("Request did not complete; inspect the target before retrying a write." & @CRLF)
Exit
EndIf
If ($oRest.ResponseStatusCode <> 202) Then
ConsoleWrite("HTTP status: " & $oRest.ResponseStatusCode & @CRLF)
ConsoleWrite("Request ID: " & $oRest.ResponseHdrByName("x-ms-request-id") & @CRLF)
; 404 is missing, 409 is conflict, and 412 is a stale ETag: none are success.
; Keep response bodies private. Reconcile uncertain writes before retrying.
Exit
EndIf
; Outer 202 only acknowledges the batch envelope, not transaction success.
$oResponseMimeText = ObjCreate("Chilkat.StringBuilder")
$bSuccess = $oResponseMimeText.Append("Content-Type: ")
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
$bSuccess = $oResponseMimeText.Append($oRest.ResponseHdrByName("Content-Type"))
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
$bSuccess = $oResponseMimeText.Append(@CRLF & "MIME-Version: 1.0" & @CRLF & @CRLF)
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
$bSuccess = $oResponseMimeText.Append($sResponseText)
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
$oEnvelope = ObjCreate("Chilkat.Mime")
$oChangeset = ObjCreate("Chilkat.Mime")
$oPart = ObjCreate("Chilkat.Mime")
$bSuccess = $oEnvelope.LoadMime($oResponseMimeText.GetAsString())
If ($bSuccess <> True) Then
ConsoleWrite("Invalid MIME batch response; reconcile before retrying." & @CRLF)
Exit
EndIf
$oTextCompare.Clear
$oTextCompare.SetString($oEnvelope.ContentType)
Local $bSameText11 = $oTextCompare.ContentsEqual("multipart/mixed",True)
If (($bSameText11 <> True) Or ($oEnvelope.NumParts <> 1)) Then
ConsoleWrite("Expected one changeset response." & @CRLF)
Exit
EndIf
$bSuccess = $oEnvelope.PartAt(0,$oChangeset)
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
$oTextCompare.Clear
$oTextCompare.SetString($oChangeset.ContentType)
Local $bSameText12 = $oTextCompare.ContentsEqual("multipart/mixed",True)
If (($bSameText12 <> True) Or ($oChangeset.NumParts < 1) Or ($oChangeset.NumParts > 2)) Then
ConsoleWrite("Invalid changeset response." & @CRLF)
Exit
EndIf
$oResponseIds = ObjCreate("Chilkat.Hashtable")
$oInner = ObjCreate("Chilkat.StringBuilder")
$oStatusLine = ObjCreate("Chilkat.StringBuilder")
$oInnerHeaders = ObjCreate("Chilkat.Mime")
Local $iPartIndex = 0
While ($iPartIndex < $oChangeset.NumParts)
$bSuccess = $oChangeset.PartAt($iPartIndex,$oPart)
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
$oTextCompare.Clear
$oTextCompare.SetString($oPart.ContentType)
Local $bSameText13 = $oTextCompare.ContentsEqual("application/http",True)
If ($bSameText13 <> True) Then
ConsoleWrite("Expected application/http response part." & @CRLF)
Exit
EndIf
$bSuccess = $oInner.SetString($oPart.GetBodyDecoded())
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
If ($oInner.Contains(@CRLF & @CRLF,True) <> True) Then
ConsoleWrite("Incomplete inner HTTP response." & @CRLF)
Exit
EndIf
$bSuccess = $oStatusLine.SetString($oInner.GetBefore(@CRLF,True))
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
If ($oStatusLine.StartsWith("HTTP/1.1 ",True) <> True) Then
ConsoleWrite("Invalid inner HTTP status line." & @CRLF)
Exit
EndIf
$bSuccess = $oInnerHeaders.LoadMime($oInner.GetAsString())
If ($bSuccess <> True) Then
ConsoleWrite("Invalid inner response headers." & @CRLF)
Exit
EndIf
Local $sContentId = $oInnerHeaders.GetHeaderField("Content-ID")
$oTextCompare.Clear
$oTextCompare.SetString($sContentId)
Local $bSameText14 = $oTextCompare.ContentsEqual("1",True)
$oTextCompare.Clear
$oTextCompare.SetString($sContentId)
Local $bSameText15 = $oTextCompare.ContentsEqual("2",True)
If (($bSameText14 <> True) And ($bSameText15 <> True)) Then
ConsoleWrite("Unexpected or missing response Content-ID." & @CRLF)
Exit
EndIf
If ($oResponseIds.Contains($sContentId) = True) Then
ConsoleWrite("Duplicate response Content-ID." & @CRLF)
Exit
EndIf
$bSuccess = $oResponseIds.AddStr($sContentId,"seen")
If ($bSuccess <> True) Then
ConsoleWrite("Could not prepare the request." & @CRLF)
Exit
EndIf
$oTextCompare.Clear
$oTextCompare.SetString($oStatusLine.GetNth(1," ",False,False))
Local $bSameText16 = $oTextCompare.ContentsEqual("204",True)
If ($bSameText16 <> True) Then
ConsoleWrite("Atomic transaction did not report success. Operation Content-ID: " & $sContentId & @CRLF)
ConsoleWrite("Inner HTTP status: " & $oStatusLine.GetNth(1," ",False,False) & @CRLF)
; Keep the full error body private. A service-reported operation failure rolls back the changeset.
Exit
EndIf
$iPartIndex = $iPartIndex + 1
Wend
If (($oChangeset.NumParts <> 2) Or ($oResponseIds.Contains("1") <> True) Or ($oResponseIds.Contains("2") <> True)) Then
ConsoleWrite("Incomplete success response; inspect state before replaying." & @CRLF)
Exit
EndIf
ConsoleWrite("Both operations succeeded in one atomic transaction." & @CRLF)