Sample code for 30+ languages & platforms
SQL Server

TicketBAI -- Send HTTP POST

See more TicketBAI Examples

Demonstrates how to send a TicketBAI POST and get the response.

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
    -- 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 @http int
    EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OAMethod @http, 'SetSslClientCertPfx', @success OUT, 'your.pfx', 'pfx_password'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        RETURN
      END

    -- Get the XML we wish to send in the body of the request.
    DECLARE @sbXml int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbXml OUT

    EXEC sp_OAMethod @sbXml, 'LoadFile', @success OUT, 'qa_data/payload.xml', 'utf-8'
    IF @success = 0
      BEGIN

        PRINT 'Failed to load XML that is to be the HTTP request body'
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @sbXml
        RETURN
      END

    -- Build the following JSON

    -- { 
    --     "con": "LROE", 
    --     "apa": "1.1", 
    --     "inte": { 
    --         "nif": "número de identificación fiscal", 
    --         "nrs": "nombre o Razón social", 
    --         "ap1": "primer apellido", 
    --         "ap2": "segundo apellido" 
    --     }, 
    --     "drs": { 
    --     "mode": "140", 
    --     "ejer": "ejercicio" 
    --     } 
    -- }

    -- Use this online tool to generate code from sample JSON: 
    -- Generate Code to Create JSON

    DECLARE @json int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT

    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'con', 'LROE'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'apa', '1.1'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'inte.nif', 'número de identificación fiscal'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'inte.nrs', 'nombre o Razón social'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'inte.ap1', 'primer apellido'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'inte.ap2', 'segundo apellido'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'drs.mode', '140'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'drs.ejer', 'ejercicio'

    -- Add required headers...
    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'eus-bizkaia-n3-version', '1.0'
    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'eus-bizkaia-n3-content-type', 'application/xml'
    EXEC sp_OAMethod @json, 'Emit', @sTmp0 OUT
    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'eus-bizkaia-n3-data', @sTmp0

    DECLARE @url nvarchar(4000)
    SELECT @url = 'https://pruesarrerak.bizkaia.eus/N3B4000M/aurkezpena'
    DECLARE @resp int
    EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT

    EXEC sp_OASetProperty @http, 'UncommonOptions', 'SendGzipped'
    EXEC sp_OAMethod @http, 'HttpSb', @success OUT, 'POST', @url, @sbXml, 'utf-8', 'application/octet-stream', @resp
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @sbXml
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @resp
        RETURN
      END

    EXEC sp_OAMethod @http, 'ClearHeaders', NULL


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

    -- Examine the response (it is already decompressed)

    PRINT 'response body:'
    EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
    PRINT @sTmp0

    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @sbXml
    EXEC @hr = sp_OADestroy @json
    EXEC @hr = sp_OADestroy @resp


END
GO