Sample code for 30+ languages & platforms
PowerBuilder

Read the REST Response Body in Chunks

See more REST Examples

Demonstrates Rest.ReadRespChunkBd, which reads the response body one chunk at a time, appending each chunk to a BinData. A return value of 0 signals the body is complete, while -1 indicates a read failure.

Background. Chunked reading lets an application process a large response incrementally rather than buffering the entire body at once. It is used after ReadResponseHeader within the staged request model.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Rest
integer li_BTls
integer li_BAutoReconnect
integer li_StatusCode
oleobject loo_SbError
oleobject loo_BdBody
integer li_ChunkResult

li_Success = 0

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
li_BTls = 1
li_BAutoReconnect = 1
li_Success = loo_Rest.Connect("example.com",443,li_BTls,li_BAutoReconnect)
if li_Success = 0 then
    Write-Debug loo_Rest.LastErrorText
    destroy loo_Rest
    return
end if

li_Success = loo_Rest.SendReqNoBody("GET","/api/largefile")
if li_Success = 0 then
    Write-Debug loo_Rest.LastErrorText
    destroy loo_Rest
    return
end if

li_StatusCode = loo_Rest.ReadResponseHeader()
if li_StatusCode < 0 then
    Write-Debug loo_Rest.LastErrorText
    destroy loo_Rest
    return
end if

//  Check for the expected success status code.  If the response is not 200, the body is likely
//  error text formatted as JSON, XML, or HTML rather than the expected content.  In that case read
//  the body as text, report it, and return.
if li_StatusCode <> 200 then
    loo_SbError = create oleobject
    li_rc = loo_SbError.ConnectToNewObject("Chilkat.StringBuilder")

    li_Success = loo_Rest.ReadRespSb(loo_SbError)
    if li_Success = 0 then
        Write-Debug loo_Rest.LastErrorText
        destroy loo_Rest
        destroy loo_SbError
        return
    end if

    Write-Debug "Request failed with status code " + string(li_StatusCode)
    Write-Debug loo_SbError.GetAsString()
    destroy loo_Rest
    destroy loo_SbError
    return
end if

//  The status code is 200.  Read the response body in chunks, appending each chunk to the BinData.
//  Each call waits until at least the requested number of bytes is available, unless the response
//  ends first.  A return value of 0 means the final bytes were returned and the body is complete;
//  -1 indicates a read failure.
loo_BdBody = create oleobject
li_rc = loo_BdBody.ConnectToNewObject("Chilkat.BinData")

li_ChunkResult = loo_Rest.ReadRespChunkBd(8192,loo_BdBody)
do while li_ChunkResult > 0
    li_ChunkResult = loo_Rest.ReadRespChunkBd(8192,loo_BdBody)
loop
if li_ChunkResult < 0 then
    Write-Debug loo_Rest.LastErrorText
    destroy loo_Rest
    destroy loo_SbError
    destroy loo_BdBody
    return
end if

Write-Debug "Received " + string(loo_BdBody.NumBytes) + " bytes."


destroy loo_Rest
destroy loo_SbError
destroy loo_BdBody