SQL Server
SQL Server
Example: Http.SetUrlVar method
Demonstrates the HTTP SetUrlVar method.Chilkat SQL Server Downloads
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
-- Important: Do not use nvarchar(max). See the warning about using nvarchar(max).
DECLARE @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
DECLARE @http int
EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
DECLARE @url nvarchar(4000)
SELECT @url = 'https://finnhub.io/api/v1/quote?symbol={$symbol}&token={$api_key}'
-- When the request is sent, the {$symbol} is replaced with "MSFT"
-- and the {$api_key} is replaced with "1234567890ABCDEF"
EXEC sp_OAMethod @http, 'SetUrlVar', @success OUT, 'symbol', 'MSFT'
EXEC sp_OAMethod @http, 'SetUrlVar', @success OUT, 'api_key', '1234567890ABCDEF'
DECLARE @sbJson int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbJson OUT
EXEC sp_OAMethod @http, 'QuickGetSb', @success OUT, @url, @sbJson
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @sbJson
RETURN
END
DECLARE @statusCode int
EXEC sp_OAGetProperty @http, 'LastStatus', @statusCode OUT
IF @statusCode <> 200
BEGIN
PRINT 'Status code: ' + @statusCode
PRINT 'Error Message:'
EXEC sp_OAMethod @sbJson, 'GetAsString', @sTmp0 OUT
PRINT @sTmp0
END
ELSE
BEGIN
PRINT 'JSON Stock Quote:'
EXEC sp_OAMethod @sbJson, 'GetAsString', @sTmp0 OUT
PRINT @sTmp0
END
-- Output:
-- JSON Stock Quote:
-- {"c":522.98,"d":0.5,"dp":0.0957,"h":524.51,"l":520.86,"o":524.28,"pc":522.48,"t":1755271948}
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @sbJson
END
GO