SQL Server
SQL Server
Http Follow Redirects
See more HTTP Examples
Demonstrates how to automatically follow redirects.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
DECLARE @iTmp0 int
-- Important: Do not use nvarchar(max). See the warning about using nvarchar(max).
DECLARE @sTmp0 nvarchar(4000)
DECLARE @http int
EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
-- If you would like to see the HTTP session log to see how
-- HTTP redirects were followed.
EXEC sp_OASetProperty @http, 'SessionLogFilename', 'c:/temp/qa_output/sessionLog.txt'
-- Run the test using this URL.
-- (Works while httpbin.org keeps the test endpoint available.)
DECLARE @url nvarchar(4000)
SELECT @url = 'https://httpbin.org/redirect-to?url={$redirectUrl}&status_code={$statusCode}'
DECLARE @success int
EXEC sp_OAMethod @http, 'SetUrlVar', @success OUT, 'redirectUrl', 'https://chilkatsoft.com/helloWorld.json'
EXEC sp_OAMethod @http, 'SetUrlVar', @success OUT, 'statusCode', '302'
DECLARE @jsonResponse nvarchar(4000)
EXEC sp_OAMethod @http, 'QuickGetStr', @jsonResponse OUT, @url
EXEC sp_OAGetProperty @http, 'LastMethodSuccess', @iTmp0 OUT
IF @iTmp0 = 0
BEGIN
EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @http
RETURN
END
EXEC sp_OAGetProperty @http, 'LastStatus', @iTmp0 OUT
PRINT 'Response status code: ' + @iTmp0
PRINT @jsonResponse
-- How to tell if the request was redirected, and if so,
-- what was the final redirect URL?
EXEC sp_OAGetProperty @http, 'WasRedirected', @iTmp0 OUT
PRINT 'Was Redirected: ' + @iTmp0
EXEC sp_OAGetProperty @http, 'FinalRedirectUrl', @sTmp0 OUT
PRINT 'Final Redirect URL: ' + @sTmp0
-- Output:
-- Response status code: 200
-- { "hello": "world" }
-- Was Redirected: True
-- Final Redirect URL: https://chilkatsoft.com/helloWorld.json
-- -----------------------------------------------
-- Here are the contents of the sessionLog.txt
-- ---- Sending Sat, 30 Aug 2025 11:41:37 GMT ----
-- GET /redirect-to?url=https%3A%2F%2Fchilkatsoft.com%2FhelloWorld.json&status_code=302 HTTP/1.1
-- Host: httpbin.org
-- Accept: */*
-- Accept-Encoding: gzip
--
--
-- ---- Received Sat, 30 Aug 2025 11:41:37 GMT ----
-- HTTP/1.1 302 FOUND
-- Date: Sat, 30 Aug 2025 11:41:38 GMT
-- Content-Type: text/html; charset=utf-8
-- Content-Length: 0
-- Connection: keep-alive
-- Server: gunicorn/19.9.0
-- Location: https://chilkatsoft.com/helloWorld.json
-- Access-Control-Allow-Origin: *
-- Access-Control-Allow-Credentials: true
--
--
-- ---- Sending Sat, 30 Aug 2025 11:41:38 GMT ----
-- GET /helloWorld.json HTTP/1.1
-- Host: chilkatsoft.com
-- Accept: */*
-- Accept-Encoding: gzip
--
--
-- ---- Received Sat, 30 Aug 2025 11:41:38 GMT ----
-- HTTP/1.1 200 OK
-- Content-Type: application/json
-- Last-Modified: Sun, 20 Aug 2023 11:36:27 GMT
-- Accept-Ranges: bytes
-- ETag: "34c27f8e5ad3d91:0"
-- Server: Microsoft-IIS/10.0
-- X-Powered-By: ASP.NET
-- Date: Sat, 30 Aug 2025 11:41:38 GMT
-- Content-Length: 22
--
-- { "hello": "world" }
EXEC @hr = sp_OADestroy @http
END
GO