Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

SII POST boleta.electronica.envio

See more SII Chile Examples

Almacenamiento de un conjunto de boletas en el SII

Chilkat Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.Http,
  Chilkat.HttpRequest,
  Chilkat.HttpResponse,
  Chilkat.StringBuilder,
  Chilkat.JsonObject;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  http: THttp;
  req: THttpRequest;
  xmlStr: string;
  resp: THttpResponse;
  sbResponseBody: TStringBuilder;
  jResp: TJsonObject;
  respStatusCode: Integer;
  rut_emisor: string;
  rut_envia: string;
  trackid: Integer;
  fecha_recepcion: string;
  estado: string;
  file: string;

begin
  success := False;

  //  This example assumes the Chilkat API to have been previously unlocked.
  //  See Global Unlock Sample for sample code.

  http := THttp.Create;

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

  req := THttpRequest.Create;
  req.HttpVerb := 'POST';
  req.Path := '/recursos/v1/boleta.electronica.envio';
  req.ContentType := 'multipart/form-data';
  req.AddParam('rutSender','66666666');
  req.AddParam('dvSender','6');
  req.AddParam('rutCompany','60803000');
  req.AddParam('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">
  //  ...
  xmlStr := '...';
  success := req.AddStringForUpload2('archivo','envioBoleta.xml',xmlStr,'ISO-8859-1','text/xml');

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

  //  For debugging, you can save the exact HTTP request sent and response received
  //  to a session log file:
  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
  resp := THttpResponse.Create;
  success := http.HttpSReq('pangal.sii.cl',443,True,req,resp);
  if (success = False) then
    begin
      WriteLn(http.LastErrorText);
      Exit;
    end;

  sbResponseBody := TStringBuilder.Create;
  resp.GetBodySb(sbResponseBody);
  jResp := TJsonObject.Create;
  jResp.LoadSb(sbResponseBody);
  jResp.EmitCompact := False;

  WriteLn('Response Body:');
  WriteLn(jResp.Emit());

  respStatusCode := resp.StatusCode;
  WriteLn('Response Status Code = ' + respStatusCode);
  if (respStatusCode >= 400) then
    begin
      WriteLn('Response Header:');
      WriteLn(resp.Header);
      WriteLn('Failed.');
      Exit;
    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

  rut_emisor := jResp.StringOf('rut_emisor');
  rut_envia := jResp.StringOf('rut_envia');
  trackid := jResp.IntOf('trackid');
  fecha_recepcion := jResp.StringOf('fecha_recepcion');
  estado := jResp.StringOf('estado');
  file := jResp.StringOf('file');


  http.Free;
  req.Free;
  resp.Free;
  sbResponseBody.Free;
  jResp.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.