Sample code for 30+ languages & platforms
SQL Server

Send a Staged REST Request with a Streamed Body

See more REST Examples

Demonstrates Rest.SendReqStreamBody, which sends a request whose body is read from a Stream, then reads the response in separate steps.

The file paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.

Background. The staged request model separates sending from reading: a SendReq method transmits the request, ReadResponseHeader reads the status line and headers, and a ReadResp method reads the body. This gives fine control over large or streamed messages compared with the single-call full-request methods.

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

    --  The file paths are relative to the application's current working directory.  Absolute paths
    --  may also be used.  Supply the paths appropriate to your own environment.

    --  The request body is read from a Stream whose source is a local file.
    DECLARE @bodyStream int
    EXEC @hr = sp_OACreate 'Chilkat.Stream', @bodyStream OUT

    EXEC sp_OASetProperty @bodyStream, 'SourceFile', 'qa_data/upload.json'

    EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'Content-Type', 'application/json'

    --  Send the request, reading the body from the stream.  The 3rd argument is the Stream.
    EXEC sp_OAMethod @rest, 'SendReqStreamBody', @success OUT, 'POST', '/api/upload', @bodyStream
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @bodyStream
        RETURN
      END

    --  After sending, read the response status line and header fields.  The return value is the HTTP
    --  status code, or -1 if no response was received.
    DECLARE @statusCode int
    EXEC sp_OAMethod @rest, 'ReadResponseHeader', @statusCode OUT
    IF @statusCode < 0
      BEGIN
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @bodyStream
        RETURN
      END

    PRINT 'Response status code: ' + @statusCode

    --  Read the response body as text.
    DECLARE @responseBody nvarchar(4000)
    EXEC sp_OAMethod @rest, 'ReadRespBodyString', @responseBody OUT
    EXEC sp_OAGetProperty @rest, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @bodyStream
        RETURN
      END

    PRINT @responseBody

    EXEC @hr = sp_OADestroy @rest
    EXEC @hr = sp_OADestroy @bodyStream


END
GO