Sample code for 30+ languages & platforms
SQL Server Requires Chilkat v11.0.0+

HTTP PUT JSON

See more HTTP Examples

Demonstrates how to send a JSON PUT and get the JSON response body.

Chilkat SQL Server Downloads

SQL Server
-- 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
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    --  This example assumes 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

    --  The PUT request to be sent will look like this:

    --  PUT /request HTTP/1.1
    --  Content-Type: application/jsonrequest
    --  Cookie: JSESSIONID=1234
    --  Content-Encoding: identity
    --  Host: json.penzance.org
    --  Accept: application/jsonrequest
    --  Accept-Encoding:
    --  Content-Length: 72
    --  
    --  {"user":"doctoravatar@penzance.com","forecast":7,"t":"vlIj","zip":94089}

    --  First, remove default header fields that would be automatically
    --  sent.  (These headers are harmless, and shouldn't need to 
    --  be suppressed, but just in case...)
    EXEC sp_OASetProperty @http, 'AcceptCharset', ''
    EXEC sp_OASetProperty @http, 'UserAgent', ''
    EXEC sp_OASetProperty @http, 'AcceptLanguage', ''

    --  Suppress the Accept-Encoding header by disallowing 
    --  a gzip response:
    EXEC sp_OASetProperty @http, 'AllowGzip', 0

    --  If a Cookie needs to be added...
    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Cookie', 'JSESSIONID=1234'

    --  Add the Content-Encoding: identity header.
    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Content-Encoding', 'identity'

    --  Modify the default "Accept" header:
    EXEC sp_OASetProperty @http, 'Accept', 'application/jsonrequest'

    DECLARE @jsonText nvarchar(4000)
    SELECT @jsonText = '{"user":"doctoravatar@penzance.com","forecast":7,"t":"vlIj","zip":94089}'

    --  IMPORTANT: Make sure to change the URL, JSON text,
    --  and other data items to your own values.  The URL used
    --  in this example will not actually work.

    DECLARE @resp int
    EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT

    EXEC sp_OAMethod @http, 'HttpStr', @success OUT, 'PUT', 'http://json.penzance.org/request', @jsonText, 'utf-8', 'application/jsonrequest', @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


    EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
    PRINT 'Response status code: ' + @iTmp0

    PRINT 'Response JSON:'
    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