SQL Server
SQL Server
Initialize an HTTP Request from a URL
See more HTTP Examples
Demonstrates how to initialize an HTTP request object from a URL.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 @sTmp1 nvarchar(4000)
DECLARE @req int
EXEC @hr = sp_OACreate 'Chilkat.HttpRequest', @req OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
-- Loads the HTTP request object from a URL
EXEC sp_OAMethod @req, 'SetFromUrl', NULL, 'https://www.youtube.com/watch?v=fAZdTchQePc&t=45s'
-- The verb will be "GET"
EXEC sp_OAGetProperty @req, 'HttpVerb', @sTmp0 OUT
PRINT 'verb = ' + @sTmp0
-- The path will be "/watch"
EXEC sp_OAGetProperty @req, 'Path', @sTmp0 OUT
PRINT 'path = ' + @sTmp0
-- The output of this loop will be
-- 0) v: fAZdTchQePc
-- 1) t: 45s
DECLARE @i int
DECLARE @numParams int
EXEC sp_OAGetProperty @req, 'NumParams', @numParams OUT
SELECT @i = 0
WHILE @i <= @numParams - 1
BEGIN
EXEC sp_OAMethod @req, 'GetParamName', @sTmp0 OUT, @i
EXEC sp_OAMethod @req, 'GetParamValue', @sTmp1 OUT, @i
PRINT @i + ') ' + @sTmp0 + ': ' + @sTmp1
SELECT @i = @i + 1
END
-- View the request that would be sent if HttpSReq is called.
-- The HTTP request will be a GET that looks like this:
--
-- GET /watch?v=fAZdTchQePc&t=45s HTTP/1.1
-- Host: domain
--
-- Note: The HOST header will automatically get filled in with the actual domain when HttpSReq is called.
EXEC sp_OAMethod @req, 'GenerateRequestText', @sTmp0 OUT
PRINT @sTmp0
-- We can add additional params
EXEC sp_OAMethod @req, 'AddParam', NULL, 'artist', 'Richard Bona'
EXEC sp_OAMethod @req, 'AddParam', NULL, 'musicType', 'Jazz'
EXEC sp_OAMethod @req, 'AddParam', NULL, 'festival', 'Estival Jazz Lugano'
EXEC sp_OAMethod @req, 'AddParam', NULL, 'year', '2008'
-- Now examine the request that would be sent
--
-- GET /watch?v=fAZdTchQePc&t=45s&artist=Richard%20Bona&musicType=Jazz&festival=Estival%20Jazz%20Lugano&year=2008 HTTP/1.1
-- Host: domain
--
EXEC sp_OAMethod @req, 'GenerateRequestText', @sTmp0 OUT
PRINT @sTmp0
-- We can remove a parameter:
EXEC sp_OAMethod @req, 'RemoveParam', NULL, 't'
EXEC sp_OAMethod @req, 'GenerateRequestText', @sTmp0 OUT
PRINT @sTmp0
-- What happens if we change the request from a GET to a POST?
EXEC sp_OASetProperty @req, 'HttpVerb', 'POST'
-- The request will now look like this:
--
-- POST /watch HTTP/1.1
-- Host: domain
-- Content-Length: 93
--
-- v=fAZdTchQePc&artist=Richard%20Bona&musicType=Jazz&festival=Estival%20Jazz%20Lugano&year=2008
--
EXEC sp_OAMethod @req, 'GenerateRequestText', @sTmp0 OUT
PRINT @sTmp0
-- What happens if we make it a multipart/form-data?
EXEC sp_OASetProperty @req, 'ContentType', 'multipart/form-data'
-- Now the POST looks like this:
--
-- POST /watch HTTP/1.1
-- Content-Type: multipart/form-data; boundary=------------020906020106050705030807
-- Host: domain
-- Content-Length: 545
--
-- --------------020906020106050705030807
-- Content-Disposition: form-data; name="v"
--
-- fAZdTchQePc
-- --------------020906020106050705030807
-- Content-Disposition: form-data; name="artist"
--
-- Richard Bona
-- --------------020906020106050705030807
-- Content-Disposition: form-data; name="musicType"
--
-- Jazz
-- --------------020906020106050705030807
-- Content-Disposition: form-data; name="festival"
--
-- Estival Jazz Lugano
-- --------------020906020106050705030807
-- Content-Disposition: form-data; name="year"
--
-- 2008
-- --------------020906020106050705030807--
EXEC sp_OAMethod @req, 'GenerateRequestText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @req
END
GO