Sample code for 30+ languages & platforms
SQL Server

Configure HTTP Basic Authentication with SecureString

See more REST Examples

Demonstrates Rest.SetAuthBasicSecure, which configures HTTP Basic authentication using SecureString objects instead of plaintext strings.

Background. A SecureString keeps sensitive values encrypted in memory, reducing the window in which credentials appear as readable plaintext. This is otherwise equivalent to SetAuthBasic.

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

    --  SetAuthBasicSecure configures HTTP Basic authentication using SecureString objects, which keep
    --  the credentials encrypted in memory.
    --  Normally you would not hard-code secrets in source.  You should instead obtain them from an
    --  interactive prompt, environment variable, or a secrets vault.
    DECLARE @ssUsername int
    EXEC @hr = sp_OACreate 'Chilkat.SecureString', @ssUsername OUT

    EXEC sp_OAMethod @ssUsername, 'Append', @success OUT, 'myUsername'
    DECLARE @ssPassword int
    EXEC @hr = sp_OACreate 'Chilkat.SecureString', @ssPassword OUT

    EXEC sp_OAMethod @ssPassword, 'Append', @success OUT, 'myPassword'

    EXEC sp_OAMethod @rest, 'SetAuthBasicSecure', @success OUT, @ssUsername, @ssPassword
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @ssUsername
        EXEC @hr = sp_OADestroy @ssPassword
        RETURN
      END

    DECLARE @responseText nvarchar(4000)
    EXEC sp_OAMethod @rest, 'FullRequestNoBody', @responseText OUT, 'GET', '/api/protected'
    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 @ssUsername
        EXEC @hr = sp_OADestroy @ssPassword
        RETURN
      END

    PRINT @responseText

    EXEC @hr = sp_OADestroy @rest
    EXEC @hr = sp_OADestroy @ssUsername
    EXEC @hr = sp_OADestroy @ssPassword


END
GO