Sample code for 30+ languages & platforms
PowerBuilder

Azure Table Insert Entity

See more Azure Table Service Examples

Insert an entity into an Azure table.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Rest
integer li_BTls
integer li_Port
integer li_BAutoReconnect
oleobject loo_AzAuth
oleobject loo_Json
oleobject loo_SbRequestBody
oleobject loo_SbResponseBody
integer li_RespStatusCode

li_Success = 0

// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

loo_Rest = create oleobject
li_rc = loo_Rest.ConnectToNewObject("Chilkat.Rest")
if li_rc < 0 then
    destroy loo_Rest
    MessageBox("Error","Connecting to COM object failed")
    return
end if

// IMPORTANT: Make sure to change "myaccount" to your actual Azure Storage Account name.
// IMPORTANT: Also change "mytable" to the name of your Azure table.
// We're going to POST to this URL:  https://myaccount.table.core.windows.net/mytable
li_BTls = 1
li_Port = 443
li_BAutoReconnect = 1
li_Success = loo_Rest.Connect("myaccount.table.core.windows.net",li_Port,li_BTls,li_BAutoReconnect)
if li_Success <> 1 then
    Write-Debug "ConnectFailReason: " + string(loo_Rest.ConnectFailReason)
    Write-Debug loo_Rest.LastErrorText
    destroy loo_Rest
    return
end if

// Provide Azure Cloud credentials for the REST call.
loo_AzAuth = create oleobject
li_rc = loo_AzAuth.ConnectToNewObject("Chilkat.AuthAzureStorage")

loo_AzAuth.AccessKey = "AZURE_ACCESS_KEY"
// The account name used here should match the 1st part of the domain passed in the call to Connect (above).
loo_AzAuth.Account = "myaccount"
loo_AzAuth.Scheme = "SharedKey"
loo_AzAuth.Service = "Table"

// This causes the "x-ms-version: 2019-07-07" header to be automatically added.
loo_AzAuth.XMsVersion = "2019-07-07"
li_Success = loo_Rest.SetAuthAzureStorage(loo_AzAuth)

// Note: The application does not need to explicitly set the following
// headers: Content-Length, x-ms-date, Authorization.  These headers
// are automatically set by Chilkat.

// Note: The above code does not need to be repeatedly called for each REST request.
// The rest object can be setup once, and then many requests can be sent.  Chilkat will automatically
// reconnect within a FullRequest* method as needed.  It is only the very first connection that is explicitly
// made via the Connect method.

// Use this online tool to generate code from sample JSON:
// Generate Code to Create JSON

// The following JSON is sent in the request body.

// {  
//    "PartitionKey":"mypartitionkey",  
//    "RowKey":"myrowkey",  
//    "Timestamp":"2013-08-22T01:12:06.2608595Z",  
//    "Address":"Mountain View",  
//    "Age":23,  
//    "AmountDue":200.23,  
//    "CustomerCode":"c9da6455-213d-42c9-9a79-3e9149a57833",  
//    "CustomerSince":"2008-07-10T00:00:00",  
//    "IsActive":true,  
//    "NumberOfOrders":"255"  
// }  

loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")

loo_Json.UpdateString("PartitionKey","mypartitionkey")
loo_Json.UpdateString("RowKey","myrowkey")
loo_Json.UpdateString("Timestamp","2013-08-22T01:12:06.2608595Z")
loo_Json.UpdateString("Address","Mountain View")
loo_Json.UpdateInt("Age",23)
loo_Json.UpdateNumber("AmountDue","200.23")
loo_Json.UpdateString("CustomerCode","c9da6455-213d-42c9-9a79-3e9149a57833")
loo_Json.UpdateString("CustomerSince","2008-07-10T00:00:00")
loo_Json.UpdateBool("IsActive",1)
loo_Json.UpdateString("NumberOfOrders","255")

// IMPORTANT: Pay attention to the options for nometadata, minimalmetadata, or fullmetadata.
// See the Azure table service API documentation at https://docs.microsoft.com/en-us/rest/api/storageservices/insert-entity
loo_Rest.AddHeader("Accept","application/json;odata=nometadata")
loo_Rest.AddHeader("Prefer","return-no-content")
loo_Rest.AddHeader("Content-Type","application/json")

loo_SbRequestBody = create oleobject
li_rc = loo_SbRequestBody.ConnectToNewObject("Chilkat.StringBuilder")

loo_Json.EmitSb(loo_SbRequestBody)
loo_SbResponseBody = create oleobject
li_rc = loo_SbResponseBody.ConnectToNewObject("Chilkat.StringBuilder")

// IMPORTANT: Change "mytable" to the name of your actual table.
li_Success = loo_Rest.FullRequestSb("POST","/mytable",loo_SbRequestBody,loo_SbResponseBody)
if li_Success <> 1 then
    Write-Debug loo_Rest.LastErrorText
    destroy loo_Rest
    destroy loo_AzAuth
    destroy loo_Json
    destroy loo_SbRequestBody
    destroy loo_SbResponseBody
    return
end if

// A status code of 204 is a success response for the case where Prefer=return-no-content.
li_RespStatusCode = loo_Rest.ResponseStatusCode
Write-Debug "Response Status Code = " + string(li_RespStatusCode)
if li_RespStatusCode >= 400 then
    Write-Debug "Response Header:"
    Write-Debug loo_Rest.ResponseHeader
    Write-Debug "Response Body:"
    Write-Debug loo_SbResponseBody.GetAsString()
    destroy loo_Rest
    destroy loo_AzAuth
    destroy loo_Json
    destroy loo_SbRequestBody
    destroy loo_SbResponseBody
    return
end if

Write-Debug "Success."


destroy loo_Rest
destroy loo_AzAuth
destroy loo_Json
destroy loo_SbRequestBody
destroy loo_SbResponseBody