Sample code for 30+ languages & platforms
SQL Server

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 SQL Server Downloads

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    DECLARE @rest int
    EXEC @hr = sp_OACreate 'Chilkat.Rest', @rest OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    DECLARE @bTls int
    SELECT @bTls = 1
    DECLARE @bAutoReconnect int
    SELECT @bAutoReconnect = 1
    EXEC sp_OAMethod @rest, 'Connect', @success OUT, 'example.com', 443, @bTls, @bAutoReconnect
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        RETURN
      END

    --  Enable debug mode so the fully composed HTTP request is captured instead of being sent.
    EXEC sp_OASetProperty @rest, 'DebugMode', 1

    --  Build a multipart request with two parts.  The captured debug output will show the multipart
    --  boundaries, part headers, and part bodies.
    EXEC sp_OASetProperty @rest, 'PartSelector', '1'
    EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'Content-Type', 'text/plain'
    EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'Content-Disposition', 'form-data; name="field1"'
    EXEC sp_OAMethod @rest, 'SetMultipartBodyString', @success OUT, 'value1'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        RETURN
      END

    EXEC sp_OASetProperty @rest, 'PartSelector', '2'
    EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'Content-Type', 'application/json'
    EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'Content-Disposition', 'form-data; name="metadata"'
    EXEC sp_OAMethod @rest, 'SetMultipartBodyString', @success OUT, '{ "active": true }'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        RETURN
      END

    --  Compose the multipart request.  In debug mode the request is built but not transmitted.
    EXEC sp_OAMethod @rest, 'SendReqMultipart', @success OUT, 'POST', '/api/upload'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        RETURN
      END

    --  Copy the composed request (request line, headers, and multipart body that would be sent) into a
    --  BinData, then view it as text.
    DECLARE @bdRequest int
    EXEC @hr = sp_OACreate 'Chilkat.BinData', @bdRequest OUT

    EXEC sp_OAMethod @rest, 'GetLastDebugRequest', @success OUT, @bdRequest

    DECLARE @requestText nvarchar(4000)
    EXEC sp_OAMethod @bdRequest, 'GetString', @requestText OUT, 'utf-8'
    EXEC sp_OAGetProperty @bdRequest, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN
        EXEC sp_OAGetProperty @bdRequest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @bdRequest
        RETURN
      END

    PRINT @requestText

    EXEC @hr = sp_OADestroy @rest
    EXEC @hr = sp_OADestroy @bdRequest


END
GO