SQL Server
SQL Server
Example: Http.HttpReq method
Demonstrates how to call the HttpReq 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
SELECT @success = 0
-- This example will send an HTTP POST with the body of the HTTP request containing JSON.
-- Create the following JSON: { "name": "Harry", "state": "FL" }
DECLARE @json int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'name', 'Harry'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'state', 'FL'
-- Specify the details of the request
DECLARE @req int
EXEC @hr = sp_OACreate 'Chilkat.HttpRequest', @req OUT
EXEC sp_OASetProperty @req, 'HttpVerb', 'POST'
EXEC sp_OASetProperty @req, 'ContentType', 'application/json'
EXEC sp_OAMethod @json, 'Emit', @sTmp0 OUT
EXEC sp_OAMethod @req, 'LoadBodyFromString', @success OUT, @sTmp0, 'utf-8'
-- Send the charset attribute in the HTTP request header.
EXEC sp_OASetProperty @req, 'SendCharset', 1
EXEC sp_OASetProperty @req, 'Charset', 'utf-8'
-- Send the POST
-- You can send the POST to the URL below.
-- The response will contain the raw body of the request that was sent.
-- (i.e. everything except the HTTP request header).
DECLARE @resp int
EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT
EXEC sp_OAMethod @http, 'HttpReq', @success OUT, 'https://www.chilkatsoft.com/echo_request_body.asp', @req, @resp
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @json
EXEC @hr = sp_OADestroy @req
EXEC @hr = sp_OADestroy @resp
RETURN
END
-- Examine the HTTP request header we sent:
EXEC sp_OAGetProperty @http, 'LastHeader', @sTmp0 OUT
PRINT @sTmp0
-- The response body contains the raw content of the HTTP request body we sent.
EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
PRINT @sTmp0
-- Sample output:
-- POST /echo_request_body.asp HTTP/1.1
-- Host: www.chilkatsoft.com
-- Content-Type: application/json; charset=utf-8
-- Content-Length: 29
--
--
-- {"name":"Harry","state":"FL"}
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @json
EXEC @hr = sp_OADestroy @req
EXEC @hr = sp_OADestroy @resp
END
GO