Sample code for 30+ languages & platforms
PureBasic

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

PureBasic
IncludeFile "CkRest.pb"
IncludeFile "CkBinData.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

    ;  Enable debug mode so the fully composed HTTP request is captured instead of being sent.
    CkRest::setCkDebugMode(rest, 1)

    ;  Build a multipart request with two parts.  The captured debug output will show the multipart
    ;  boundaries, part headers, and part bodies.
    CkRest::setCkPartSelector(rest, "1")
    CkRest::ckAddHeader(rest,"Content-Type","text/plain")
    CkRest::ckAddHeader(rest,"Content-Disposition","form-data; name=" + Chr(34) + "field1" + Chr(34))
    success = CkRest::ckSetMultipartBodyString(rest,"value1")
    If success = 0
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        ProcedureReturn
    EndIf

    CkRest::setCkPartSelector(rest, "2")
    CkRest::ckAddHeader(rest,"Content-Type","application/json")
    CkRest::ckAddHeader(rest,"Content-Disposition","form-data; name=" + Chr(34) + "metadata" + Chr(34))
    success = CkRest::ckSetMultipartBodyString(rest,"{ " + Chr(34) + "active" + Chr(34) + ": true }")
    If success = 0
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        ProcedureReturn
    EndIf

    ;  Compose the multipart request.  In debug mode the request is built but not transmitted.
    success = CkRest::ckSendReqMultipart(rest,"POST","/api/upload")
    If success = 0
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        ProcedureReturn
    EndIf

    ;  Copy the composed request (request line, headers, and multipart body that would be sent) into a
    ;  BinData, then view it as text.
    bdRequest.i = CkBinData::ckCreate()
    If bdRequest.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkRest::ckGetLastDebugRequest(rest,bdRequest)

    requestText.s = CkBinData::ckGetString(bdRequest,"utf-8")
    If CkBinData::ckLastMethodSuccess(bdRequest) = 0
        Debug CkBinData::ckLastErrorText(bdRequest)
        CkRest::ckDispose(rest)
        CkBinData::ckDispose(bdRequest)
        ProcedureReturn
    EndIf

    Debug requestText


    CkRest::ckDispose(rest)
    CkBinData::ckDispose(bdRequest)


    ProcedureReturn
EndProcedure