uses
IdHTTP, IdSSLOpenSSL;
const
CRLF = #13#10;
procedure TForm1.Button1Click(Sender: TObject);
var
sl: TStringList;
begin
sl := TStringList.Create;
if PostXmlFile('https://api.demoeotpremnica.mfin.gov.rs/public/documents/requests', 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx', '20250310-001', 'd:\test.xml', sl) then
ShowMessage('Response:' + CRLF + sl.Text)
else
ShowMessage('Error:' + CRLF + sl.Text);
sl.Free;
end;
function TForm1.PostXmlFile(url, apikey, requestid, xmlfilename: String; var outdata: TStringList): Boolean;
var
hc: TIdHTTP;
hcssl : TIdSSLIOHandlerSocketOpenSSL;
b, s: String;
sl: TStringList;
msin, msout: TMemoryStream;
begin
outdata.Clear;
Result := False;
if not FileExists(xmlfilename) then
raise Exception.Create('File not exists!');
sl := TStringList.Create;
sl.LoadFromFile(xmlfilename);
b := IntToHex(Random(MaxInt), 8);
//IdOpenSSLSetLibPath(IPat); // path to ssl libs
msin := TMemoryStream.Create;
msout := TMemoryStream.Create;
hc := TIdHTTP.Create;
hcssl := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
try
hcssl.SSLOptions.Mode := sslmClient;
hcssl.SSLOptions.SSLVersions := [sslvTLSv1_1,sslvTLSv1_2];
hc.IOHandler := hcssl;
//request body
s := '--' + b + CRLF;
s := s + 'Content-Disposition: form-data; name="RequestId"' + CRLF + CRLF + requestid + CRLF;
s := s + '--' + b + CRLF;
s := s + 'Content-Disposition: form-data; name="File"; filename="' + xmlfilename +'"' + CRLF;
s := s + 'Content-Type: text/xml' + CRLF + CRLF;
s := s + sl.Text;
s := s + CRLF + '--' + b + '--' + CRLF;
msin.Write(s[1], Length(s));
msin.Position := 0;
//
hc.HandleRedirects := True;
hc.ReadTimeout := 30000;
hc.Request.Accept := '*/*';
hc.Request.ContentType := 'multipart/form-data; boundary=' + b;
hc.Request.CustomHeaders.AddValue('Api-key', 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx');
hc.HTTPOptions := hc.HTTPOptions + [hoNoProtocolErrorException];
hc.Post(url, msin, msout);
msout.Position := 0;
Result := (hc.ResponseCode div 100) = 2;
outdata.LoadFromStream(msout);
if not Result then
outdata.Text := Format('[%d] - %s > %s', [hc.ResponseCode, hc.ResponseText, outdata.Text]);
finally
hcssl.Free;
hc.Free;
sl.Free;
msin.Free;
msout.Free;
end;
end;