Sample code for 30+ languages & platforms
PureBasic

Read the REST Response Body into a StringBuilder

See more REST Examples

Demonstrates Rest.ReadRespSb, which reads the response body as text into a StringBuilder, replacing its previous contents.

Background. The staged request model separates sending from reading: a SendReq method transmits the request, ReadResponseHeader reads the status line and headers, and a ReadResp method reads the body. This gives fine control over large or streamed messages compared with the single-call full-request methods.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkRest.pb"
IncludeFile "CkStringBuilder.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/items/123")
    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

    ;  Read the response body as text into a StringBuilder, replacing its previous contents.
    sbResponse.i = CkStringBuilder::ckCreate()
    If sbResponse.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

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

    responseBody.s = CkStringBuilder::ckGetAsString(sbResponse)
    If CkStringBuilder::ckLastMethodSuccess(sbResponse) = 0
        Debug CkStringBuilder::ckLastErrorText(sbResponse)
        CkRest::ckDispose(rest)
        CkStringBuilder::ckDispose(sbResponse)
        ProcedureReturn
    EndIf

    Debug responseBody


    CkRest::ckDispose(rest)
    CkStringBuilder::ckDispose(sbResponse)


    ProcedureReturn
EndProcedure