Sample code for 30+ languages & platforms
PureBasic

Get a REST Response Header Value by Index

See more REST Examples

Demonstrates Rest.ResponseHdrValue, which returns the value of the response header at a zero-based index. The same index used with ResponseHdrName gives the corresponding field name.

Background. Response headers can be enumerated by index from 0 through NumResponseHeaders minus one, after ReadResponseHeader has been called.

Chilkat PureBasic Downloads

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

    ;  Return the value of the response header at a zero-based index.  Use the same index with
    ;  ResponseHdrName to get the corresponding field name.
    numHeaders.i = CkRest::ckNumResponseHeaders(rest)
    i.i
    For i = 0 To numHeaders - 1
        value.s = CkRest::ckResponseHdrValue(rest,i)
        If CkRest::ckLastMethodSuccess(rest) = 0
            Debug CkRest::ckLastErrorText(rest)
            CkRest::ckDispose(rest)
            ProcedureReturn
        EndIf

        Debug "Header " + Str(i) + " value: " + value
    Next


    CkRest::ckDispose(rest)


    ProcedureReturn
EndProcedure