Sample code for 30+ languages & platforms
PureBasic

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 PureBasic Downloads

PureBasic
IncludeFile "CkBinData.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkRest.pb"

Procedure ChilkatExample()

    success.i = 0

    rest.i = CkRest::ckCreate()
    If rest.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    bTls.i = 1
    bAutoReconnect.i = 1
    success = CkRest::ckConnect(rest,"example.com",443,bTls,bAutoReconnect)
    If success = 0
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        ProcedureReturn
    EndIf

    success = CkRest::ckSendReqNoBody(rest,"GET","/api/largefile")
    If success = 0
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        ProcedureReturn
    EndIf

    statusCode.i = CkRest::ckReadResponseHeader(rest)
    If statusCode < 0
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        ProcedureReturn
    EndIf

    ;  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 statusCode <> 200
        sbError.i = CkStringBuilder::ckCreate()
        If sbError.i = 0
            Debug "Failed to create object."
            ProcedureReturn
        EndIf

        success = CkRest::ckReadRespSb(rest,sbError)
        If success = 0
            Debug CkRest::ckLastErrorText(rest)
            CkRest::ckDispose(rest)
            CkStringBuilder::ckDispose(sbError)
            ProcedureReturn
        EndIf

        Debug "Request failed with status code " + Str(statusCode)
        Debug CkStringBuilder::ckGetAsString(sbError)
        CkRest::ckDispose(rest)
        CkStringBuilder::ckDispose(sbError)
        ProcedureReturn
    EndIf

    ;  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.
    bdBody.i = CkBinData::ckCreate()
    If bdBody.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    chunkResult.i = CkRest::ckReadRespChunkBd(rest,8192,bdBody)
    While chunkResult > 0
        chunkResult = CkRest::ckReadRespChunkBd(rest,8192,bdBody)
    Wend
    If chunkResult < 0
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        CkStringBuilder::ckDispose(sbError)
        CkBinData::ckDispose(bdBody)
        ProcedureReturn
    EndIf

    Debug "Received " + Str(CkBinData::ckNumBytes(bdBody)) + " bytes."


    CkRest::ckDispose(rest)
    CkStringBuilder::ckDispose(sbError)
    CkBinData::ckDispose(bdBody)


    ProcedureReturn
EndProcedure