SQL Server
SQL Server
HTTP POST JSON (application/json)
See more HTTP Examples
Demonstrates how to send a JSON POST using the application/json content-type.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
-- This example requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
DECLARE @req int
EXEC @hr = sp_OACreate 'Chilkat.HttpRequest', @req OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
DECLARE @http int
EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
-- If any custom headers need to be included with the POST, add them
-- by calling SetRequestHeader
-- Note: An application should never explicitly set the Content-Length header.
-- The Content-Length is automatically computed and added by Chilkat.
-- Here are some examples of custom headers.
-- Perhaps your particular app needs some sort of custom-computed Authorization header...
EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Authorization', 'my-custom-computed-auth-value'
-- Another custom header for some hypothetical app:
EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'X-Pass-Timestamp', 'my-custom-computed-timestamp-value'
-- The following "Accept" header may be set, but it really isn't necessary:
EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Accept', 'application/json'
DECLARE @jsonText nvarchar(4000)
SELECT @jsonText = '{ some JSON text ... }'
-- To use SSL/TLS, simply use "https://" in the URL.
DECLARE @resp int
EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT
EXEC sp_OAMethod @http, 'HttpStr', @success OUT, 'POST', 'http://www.someserver.com/someJsonEndpoint', @jsonText, 'utf-8', 'application/json', @resp
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @req
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
RETURN
END
-- Display the JSON response.
EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @req
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
END
GO