Sample code for 30+ languages & platforms
DataFlex

Read the REST Response Header and Status Code

See more REST Examples

Demonstrates Rest.ReadResponseHeader, which reads the HTTP response status line and header fields after a staged send. The returned integer is the HTTP status code, or -1 if no response was received.

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

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoRest
    Boolean iBTls
    Boolean iBAutoReconnect
    Integer iStatusCode
    String sTemp1

    Move False To iSuccess

    Get Create (RefClass(cComChilkatRest)) To hoRest
    If (Not(IsComObjectCreated(hoRest))) Begin
        Send CreateComObject of hoRest
    End
    Move True To iBTls
    Move True To iBAutoReconnect
    Get ComConnect Of hoRest "example.com" 443 iBTls iBAutoReconnect To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoRest To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComSendReqNoBody Of hoRest "GET" "/api/items/123" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoRest To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Read the HTTP response status line and header fields.  The returned integer is the HTTP status
    //  code (such as 200, 404, or 500), or -1 if no response was received.
    Get ComReadResponseHeader Of hoRest To iStatusCode
    If (iStatusCode < 0) Begin
        Get ComLastErrorText Of hoRest To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "Response status code: " iStatusCode


End_Procedure