Sample code for 30+ languages & platforms
SQL Server

SII POST boleta.electronica.envio

See more SII Chile Examples

Almacenamiento de un conjunto de boletas en el SII

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

    -- Implements the following CURL command:

    -- curl -X POST "https://pangal.sii.cl/recursos/v1/boleta.electronica.envio" -H  "accept: application/json" 
    --   -H  "User-Agent: Mozilla/4.0 ( compatible; PROG 1.0; Windows NT)"
    --   -H  "Cookie: YZVQNQY4J5DT9" -H  "Content-Type: multipart/form-data"
    --   -F "rutSender=1234" -F "dvSender=xyz" -F "rutCompany=9999" -F "dvCompany=abc"
    --   -F "archivo=@starfish20.jpg;type=image/jpeg"

    -- Use the following online tool to generate HTTP code from a CURL command
    -- Convert a cURL Command to HTTP Source Code

    DECLARE @req int
    EXEC @hr = sp_OACreate 'Chilkat.HttpRequest', @req OUT

    EXEC sp_OASetProperty @req, 'HttpVerb', 'POST'
    EXEC sp_OASetProperty @req, 'Path', '/recursos/v1/boleta.electronica.envio'
    EXEC sp_OASetProperty @req, 'ContentType', 'multipart/form-data'
    EXEC sp_OAMethod @req, 'AddParam', NULL, 'rutSender', '66666666'
    EXEC sp_OAMethod @req, 'AddParam', NULL, 'dvSender', '6'
    EXEC sp_OAMethod @req, 'AddParam', NULL, 'rutCompany', '60803000'
    EXEC sp_OAMethod @req, 'AddParam', NULL, 'dvCompany', 'K'

    -- Add an XML file that contains something like this:
    --   <?xml version="1.0" encoding="ISO-8859-1"?>
    --   <EnvioBOLETA version="1.0" xmlns="http://www.sii.cl/SiiDte" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sii.cl/SiiDte EnvioBOLETA_v11.xsd">
    --     <SetDTE ID="SetDocB0T39_20201103_131999">
    --       <Caratula version="1.0">
    -- ...
    DECLARE @xmlStr nvarchar(4000)
    SELECT @xmlStr = '...'
    EXEC sp_OAMethod @req, 'AddStringForUpload2', @success OUT, 'archivo', 'envioBoleta.xml', @xmlStr, 'ISO-8859-1', 'text/xml'

    EXEC sp_OAMethod @req, 'AddHeader', NULL, 'Expect', '100-continue'
    EXEC sp_OAMethod @req, 'AddHeader', NULL, 'User-Agent', 'Mozilla/4.0 ( compatible; PROG 1.0; Windows NT)'
    EXEC sp_OAMethod @req, 'AddHeader', NULL, 'Cookie', 'TOKEN=YZVQNQY4J5DT9'
    EXEC sp_OAMethod @req, 'AddHeader', NULL, 'accept', 'application/json'
    EXEC sp_OAMethod @req, 'AddHeader', NULL, 'Content-Type', 'multipart/form-data'

    -- For debugging, you can save the exact HTTP request sent and response received
    -- to a session log file:
    EXEC sp_OASetProperty @http, 'SessionLogFilename', 'someDir/sessionLog.txt'

    -- Use one of the following domains, depending on the environment:
    -- pangal.sii.cl  - Certification Environment
    -- rahue.sii.cl      - Production Environment
    DECLARE @resp int
    EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT

    EXEC sp_OAMethod @http, 'HttpSReq', @success OUT, 'pangal.sii.cl', 443, 1, @req, @resp
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @req
        EXEC @hr = sp_OADestroy @resp
        RETURN
      END

    DECLARE @sbResponseBody int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbResponseBody OUT

    EXEC sp_OAMethod @resp, 'GetBodySb', @success OUT, @sbResponseBody
    DECLARE @jResp int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jResp OUT

    EXEC sp_OAMethod @jResp, 'LoadSb', @success OUT, @sbResponseBody
    EXEC sp_OASetProperty @jResp, 'EmitCompact', 0


    PRINT 'Response Body:'
    EXEC sp_OAMethod @jResp, 'Emit', @sTmp0 OUT
    PRINT @sTmp0

    DECLARE @respStatusCode int
    EXEC sp_OAGetProperty @resp, 'StatusCode', @respStatusCode OUT

    PRINT 'Response Status Code = ' + @respStatusCode
    IF @respStatusCode >= 400
      BEGIN

        PRINT 'Response Header:'
        EXEC sp_OAGetProperty @resp, 'Header', @sTmp0 OUT
        PRINT @sTmp0

        PRINT 'Failed.'
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @req
        EXEC @hr = sp_OADestroy @resp
        EXEC @hr = sp_OADestroy @sbResponseBody
        EXEC @hr = sp_OADestroy @jResp
        RETURN
      END

    -- Sample JSON response:
    -- (Sample code for parsing the JSON response is shown below)

    -- {
    --   "rut_emisor": "45000054-K",
    --   "rut_envia": "83154595-0",
    --   "trackid": 1014,
    --   "fecha_recepcion": "2020-09-01 20:30:10",
    --   "estado": "REC",
    --   "file": "boleta-2020-09-01-001.xml"
    -- }

    -- Sample code for parsing the JSON response...
    -- Use the following online tool to generate parsing code from sample JSON:
    -- Generate Parsing Code from JSON

    DECLARE @rut_emisor nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @rut_emisor OUT, 'rut_emisor'
    DECLARE @rut_envia nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @rut_envia OUT, 'rut_envia'
    DECLARE @trackid int
    EXEC sp_OAMethod @jResp, 'IntOf', @trackid OUT, 'trackid'
    DECLARE @fecha_recepcion nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @fecha_recepcion OUT, 'fecha_recepcion'
    DECLARE @estado nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @estado OUT, 'estado'
    DECLARE @file nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @file OUT, 'file'

    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @req
    EXEC @hr = sp_OADestroy @resp
    EXEC @hr = sp_OADestroy @sbResponseBody
    EXEC @hr = sp_OADestroy @jResp


END
GO