Sample code for 30+ languages & platforms
DataFlex

Capture the Composed REST Request for Debugging

See more REST Examples

Demonstrates Rest.GetLastDebugRequest, which copies the fully composed HTTP request (request line, headers, and body) into a BinData. The example builds a two-part multipart request so the captured output shows the multipart boundaries, part headers, and part bodies. This works when the DebugMode property was enabled while the request was composed.

Background. When DebugMode is enabled, Chilkat builds the request but does not transmit it, so GetLastDebugRequest can reveal the exact bytes that would be sent. Inspecting the composed request is valuable for diagnosing API problems.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoRest
    Boolean iBTls
    Boolean iBAutoReconnect
    Variant vBdRequest
    Handle hoBdRequest
    String sRequestText
    String sTemp1
    Boolean bTemp1

    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

    //  Enable debug mode so the fully composed HTTP request is captured instead of being sent.
    Set ComDebugMode Of hoRest To True

    //  Build a multipart request with two parts.  The captured debug output will show the multipart
    //  boundaries, part headers, and part bodies.
    Set ComPartSelector Of hoRest To "1"
    Get ComAddHeader Of hoRest "Content-Type" "text/plain" To iSuccess
    Get ComAddHeader Of hoRest "Content-Disposition" 'form-data; name="field1"' To iSuccess
    Get ComSetMultipartBodyString Of hoRest "value1" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoRest To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Set ComPartSelector Of hoRest To "2"
    Get ComAddHeader Of hoRest "Content-Type" "application/json" To iSuccess
    Get ComAddHeader Of hoRest "Content-Disposition" 'form-data; name="metadata"' To iSuccess
    Get ComSetMultipartBodyString Of hoRest '{ "active": true }' To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoRest To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Compose the multipart request.  In debug mode the request is built but not transmitted.
    Get ComSendReqMultipart Of hoRest "POST" "/api/upload" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoRest To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Copy the composed request (request line, headers, and multipart body that would be sent) into a
    //  BinData, then view it as text.
    Get Create (RefClass(cComChilkatBinData)) To hoBdRequest
    If (Not(IsComObjectCreated(hoBdRequest))) Begin
        Send CreateComObject of hoBdRequest
    End
    Get pvComObject of hoBdRequest to vBdRequest
    Get ComGetLastDebugRequest Of hoRest vBdRequest To iSuccess

    Get ComGetString Of hoBdRequest "utf-8" To sRequestText
    Get ComLastMethodSuccess Of hoBdRequest To bTemp1
    If (bTemp1 = False) Begin
        Get ComLastErrorText Of hoBdRequest To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln sRequestText


End_Procedure