Sample code for 30+ languages & platforms
Lianja

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

Lianja
llSuccess = .F.

loRest = createobject("CkRest")
llBTls = .T.
llBAutoReconnect = .T.
llSuccess = loRest.Connect("example.com",443,llBTls,llBAutoReconnect)
if (llSuccess = .F.) then
    ? loRest.LastErrorText
    release loRest
    return
endif

//  Enable debug mode so the fully composed HTTP request is captured instead of being sent.
loRest.DebugMode = .T.

//  Build a multipart request with two parts.  The captured debug output will show the multipart
//  boundaries, part headers, and part bodies.
loRest.PartSelector = "1"
loRest.AddHeader("Content-Type","text/plain")
loRest.AddHeader("Content-Disposition",'form-data; name="field1"')
llSuccess = loRest.SetMultipartBodyString("value1")
if (llSuccess = .F.) then
    ? loRest.LastErrorText
    release loRest
    return
endif

loRest.PartSelector = "2"
loRest.AddHeader("Content-Type","application/json")
loRest.AddHeader("Content-Disposition",'form-data; name="metadata"')
llSuccess = loRest.SetMultipartBodyString('{ "active": true }')
if (llSuccess = .F.) then
    ? loRest.LastErrorText
    release loRest
    return
endif

//  Compose the multipart request.  In debug mode the request is built but not transmitted.
llSuccess = loRest.SendReqMultipart("POST","/api/upload")
if (llSuccess = .F.) then
    ? loRest.LastErrorText
    release loRest
    return
endif

//  Copy the composed request (request line, headers, and multipart body that would be sent) into a
//  BinData, then view it as text.
loBdRequest = createobject("CkBinData")
loRest.GetLastDebugRequest(loBdRequest)

lcRequestText = loBdRequest.GetString("utf-8")
if (loBdRequest.LastMethodSuccess = .F.) then
    ? loBdRequest.LastErrorText
    release loRest
    release loBdRequest
    return
endif

? lcRequestText


release loRest
release loBdRequest