Sample code for 30+ languages & platforms
PowerBuilder

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

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Rest
integer li_BTls
integer li_BAutoReconnect
oleobject loo_BdRequest
string ls_RequestText

li_Success = 0

loo_Rest = create oleobject
li_rc = loo_Rest.ConnectToNewObject("Chilkat.Rest")
if li_rc < 0 then
    destroy loo_Rest
    MessageBox("Error","Connecting to COM object failed")
    return
end if
li_BTls = 1
li_BAutoReconnect = 1
li_Success = loo_Rest.Connect("example.com",443,li_BTls,li_BAutoReconnect)
if li_Success = 0 then
    Write-Debug loo_Rest.LastErrorText
    destroy loo_Rest
    return
end if

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

//  Build a multipart request with two parts.  The captured debug output will show the multipart
//  boundaries, part headers, and part bodies.
loo_Rest.PartSelector = "1"
loo_Rest.AddHeader("Content-Type","text/plain")
loo_Rest.AddHeader("Content-Disposition","form-data; name=~"field1~"")
li_Success = loo_Rest.SetMultipartBodyString("value1")
if li_Success = 0 then
    Write-Debug loo_Rest.LastErrorText
    destroy loo_Rest
    return
end if

loo_Rest.PartSelector = "2"
loo_Rest.AddHeader("Content-Type","application/json")
loo_Rest.AddHeader("Content-Disposition","form-data; name=~"metadata~"")
li_Success = loo_Rest.SetMultipartBodyString("{ ~"active~": true }")
if li_Success = 0 then
    Write-Debug loo_Rest.LastErrorText
    destroy loo_Rest
    return
end if

//  Compose the multipart request.  In debug mode the request is built but not transmitted.
li_Success = loo_Rest.SendReqMultipart("POST","/api/upload")
if li_Success = 0 then
    Write-Debug loo_Rest.LastErrorText
    destroy loo_Rest
    return
end if

//  Copy the composed request (request line, headers, and multipart body that would be sent) into a
//  BinData, then view it as text.
loo_BdRequest = create oleobject
li_rc = loo_BdRequest.ConnectToNewObject("Chilkat.BinData")

loo_Rest.GetLastDebugRequest(loo_BdRequest)

ls_RequestText = loo_BdRequest.GetString("utf-8")
if loo_BdRequest.LastMethodSuccess = 0 then
    Write-Debug loo_BdRequest.LastErrorText
    destroy loo_Rest
    destroy loo_BdRequest
    return
end if

Write-Debug ls_RequestText


destroy loo_Rest
destroy loo_BdRequest