Recent

Author Topic: Pleas help, Synapse / Indy HttpSend problem  (Read 2118 times)

patyit

  • New Member
  • *
  • Posts: 33
Pleas help, Synapse / Indy HttpSend problem
« on: January 01, 2026, 01:06:21 pm »
Hi all !
Please help implementig HTTP Rest API belowe:
Code: Pascal  [Select][+][-]
  1. curl -X ‘POST’ \ ‘https://api.demoeotpremnica.mfin.gov.rs/public/documents/requests’\
  2. -H ‘accept: */*’ \
  3. -H ‘Api-key: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx’\
  4. -H ’Content-Type:multipart/form-data’\
  5. -F ’RequestId=20250310-001’\
  6. -F ’File=@despatch-advice0603.xml;type=text/xml’
With Synapse THTTPSend :
Code: Pascal  [Select][+][-]
  1. function EUpload(ubl: string): boolean;
  2. const
  3.   CRLF = #13#10;
  4. var
  5.   Htt : THTTPSend;
  6.   fst : TFileStream;
  7.   url, res, bou, s : string;
  8. begin
  9.   Result := False;
  10.   fst := TFileStream.Create(ubl, fmOpenRead);
  11.   Htt := THTTPSend.Create;
  12.   try
  13.     Htt.Protocol := '1.1';
  14.     Htt.UserAgent:= 'Mozilla/5.0';
  15.  
  16.     bou := IntToHex(Random(MaxInt), 8)+'_Synapse_boundary';
  17.     Htt.MimeType := 'multipart/form-data; boundary='+bou;
  18.  
  19.     s := '--'+bou+CRLF+
  20.          'Content-Disposition: form-data; name="RequestId"'+CRLF+
  21.          'Content-Type: text/plain'+CRLF+CRLF;
  22.     WriteStrToStream(Htt.Document, s);
  23.     WriteStrToStream(Htt.Document, '20250310-001');
  24.  
  25.     s := CRLF+'--'+bou+CRLF+
  26.         'Content-Disposition: form-data; name="File"; '+
  27.         'filename="despatch-advice0603.xml"'+CRLF+
  28.         'Content-Type: text/xml'+CRLF+CRLF;
  29.     WriteStrToStream(Htt.Document, s);
  30.     Htt.Document.CopyFrom(fst, 0);
  31.     WriteStrToStream(Htt.Document, CRLF+'--'+bou+'--'+CRLF);
  32.  
  33.     Htt.MimeType := 'multipart/form-data; boundary='+bou;
  34.     Htt.Headers.Add('Content-Type: multipart/form-data');
  35.     Htt.Headers.Add('accept: */*');
  36.     Htt.Headers.Add('Api-key: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx');  //
  37.  
  38.     url := 'https://api.demoeotpremnica.mfin.gov.rs/public/documents/requests';
  39.     if Htt.HTTPMethod('POST', url) then begin
  40.       res := ReadStrFromStream(Htt.Document, Htt.Document.Size);
  41.       if Htt.ResultCode = 200 then
  42.         Result := True
  43.       else
  44.         MessageDlg('Error: ('+IntToStr(Htt.ResultCode)+') '+#13+res+#13#13+
  45.                                             Htt.ResultString, mtError, [mbOk], 0);
  46.     end else
  47.       MessageDlg('Error, check internet connection ...', mtError, [mbOk], 0);
  48.   finally
  49.     Htt.Free;
  50.     fst.Free;
  51.   end;
  52. end;
     
Abawe cod reporter errod :
Code: Pascal  [Select][+][-]
  1. Error: (400)
  2. {"type":"https://tools.ietf.org/html/rfc9110#section-15.5.1","title":"One or more validation errors occurred.","status":400,"errors":[{"code":"Validation","propertyName":"File","detail":"The File field is required."},{"code":"Validation","propertyName":"RequestId","detail":"The RequestId field is required."}],"traceId":"00-1e937cad8ee7c611d8505e962f92094e-317152e86f46437f-01"}
  3.  
  4. Bad Request
With Indy TIdHTTP :
Code: Pascal  [Select][+][-]
  1. function TFDataMod.EOtprUpload(ubl, rid: string): boolean;
  2. var
  3.   idHttp : TIdHTTP;
  4.   idHSSL : TIdSSLIOHandlerSocketOpenSSL;
  5.   pod : TIdMultipartFormDataStream;
  6.   url, res, cir : string;
  7. begin
  8.   Result := False;
  9.  
  10.   IdOpenSSLSetLibPath(IPat); // path to ssl libs
  11.   idHttp := TIdHTTP.Create;
  12.   try
  13.     idHSSL := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
  14.     try
  15.       idHSSL.SSLOptions.Mode := sslmClient;
  16.       idHSSL.SSLOptions.SSLVersions := [sslvTLSv1_1,sslvTLSv1_2];
  17.       idHttp.IOHandler := idHSSL;
  18.       idHttp.HandleRedirects := True;
  19.       idHttp.ReadTimeout     := 30000;
  20.       idHttp.Request.Accept  := '*/*';
  21.       idHttp.Request.ContentType := 'multipart/form-data';
  22.       idHttp.Request.CustomHeaders.AddValue('Api-key', 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx');
  23.       idHttp.HTTPOptions := idHttp.HTTPOptions+[hoNoProtocolErrorException];
  24.       pod := TIdMultipartFormDataStream.Create;
  25.       try
  26.         pod.AddFormField('RequestId', rid); // reqiestid 20250310-001
  27.         pod.AddFile('File', ubl, 'text/xml'); // ubl file despatch-advice0603.xml
  28.         url := 'https://api.demoeotpremnica.mfin.gov.rs/public/documents/requests';
  29.         try
  30.           res := idHttp.Post(url, pod);
  31.           if idHttp.ResponseCode = 200 then
  32.             Result := True
  33.           else
  34.             MessageDlg('Error: '+IntToStr(idHttp.ResponseCode)+' '+
  35.                         idHttp.ResponseText, mtError, [mbOk], 0);
  36.         except
  37.           on E: Exception do begin
  38.             MessageDlg('Error, check internet connection ...'+#13#13+
  39.                         E.Message, mtError, [mbOk], 0);
  40.           end;
  41.         end;
  42.       finally
  43.         pod.Free;
  44.       end;
  45.     finally
  46.       idHSSL.Free;
  47.     end;
  48.   finally
  49.     idHttp.Free;
  50.   end;
  51. end;
Abawe cod reporter errod :
Code: Pascal  [Select][+][-]
  1. Error: 405 HTTP/1.1 405 Not Allowed
I would be grateful if anyone has any ideas what the problem could be !
« Last Edit: January 01, 2026, 01:17:34 pm by patyit »

Thaddy

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Re: Pleas help, Synapse / Indy HttpSend problem
« Reply #1 on: January 01, 2026, 02:09:00 pm »
Wrong ssl dll's as usual. Too old.
« Last Edit: January 01, 2026, 02:10:32 pm by Thaddy »
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

paweld

  • Hero Member
  • *****
  • Posts: 1568
Re: Pleas help, Synapse / Indy HttpSend problem
« Reply #2 on: January 01, 2026, 02:26:37 pm »
for synapse:
Code: Pascal  [Select][+][-]
  1. uses
  2.   httpsend, ssl_openssl3, ssl_openssl3_lib, synautil, blcksock, synachar;
  3.  
  4. procedure TForm1.Button1Click(Sender: TObject);
  5. var
  6.   sl: TStringList;
  7. begin
  8.   sl := TStringList.Create;
  9.   if PostXmlFile('https://api.demoeotpremnica.mfin.gov.rs/public/documents/requests', 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx', '20250310-001', 'd:\test.xml', sl) then
  10.     ShowMessage('Response:' + CRLF + sl.Text)
  11.   else
  12.     ShowMessage('Error:' + CRLF + sl.Text);
  13.   sl.Free;
  14. end;
  15.  
  16. function TForm1.PostXmlFile(url, apikey, requestid, xmlfilename: String; var outdata: TStringList): Boolean;
  17. var
  18.   hc: THTTPSend;
  19.   b, s: String;
  20.   sl: TStringList;
  21. begin
  22.   outdata.Clear;
  23.   Result := False;
  24.   if not FileExists(xmlfilename) then
  25.     raise Exception.Create('File not exists!');
  26.   sl := TStringList.Create;
  27.   sl.LoadFromFile(xmlfilename);
  28.   b := IntToHex(Random(MaxInt), 8);
  29.   hc := THTTPSend.Create;
  30.   try
  31.     s := '--' + b + CRLF;
  32.     s := s + 'Content-Disposition: form-data; name="RequestId"' + CRLF + CRLF + requestid + CRLF;
  33.     s := s + '--' + b + CRLF;
  34.     s := s + 'Content-Disposition: form-data; name="File"; filename="' + xmlfilename +'"' + CRLF;
  35.     s := s + 'Content-Type: text/xml' + CRLF + CRLF;
  36.     s := s + sl.Text;
  37.     s := s + CRLF + '--' + b + '--' + CRLF;
  38.     hc.Document.Clear;
  39.     hc.Document.Write(s[1], Length(s));
  40.     hc.Document.Position := 0;
  41.     hc.MimeType := 'multipart/form-data; boundary=' + b;
  42.     hc.Headers.Insert(0, 'Accept: */*');
  43.     hc.Headers.Add('Api-key: ' + apikey);
  44.     hc.KeepAlive := False;
  45.     Result := hc.HTTPMethod('POST', URL);
  46.     if Result and ((hc.ResultCode div 100) <> 2) then
  47.       Result := False;
  48.     if Result then
  49.       outdata.LoadFromStream(hc.Document)
  50.     else
  51.     begin
  52.       outdata.LoadFromStream(hc.Document);
  53.       outdata.Text := Format('[%d] - %s > %s', [hc.ResultCode, hc.ResultString, outdata.Text]);
  54.     end;
  55.   finally
  56.     hc.Free;
  57.     sl.Free;
  58.   end;
  59. end;
« Last Edit: January 01, 2026, 02:32:31 pm by paweld »
Best regards / Pozdrawiam
paweld

Thaddy

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Re: Pleas help, Synapse / Indy HttpSend problem
« Reply #3 on: January 01, 2026, 03:25:47 pm »
Yes, but that still assumes correct and current openssl libraries.
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

patyit

  • New Member
  • *
  • Posts: 33
Re: Pleas help, Synapse / Indy HttpSend problem
« Reply #4 on: January 01, 2026, 06:18:06 pm »
Hi, Paweld and Thaddy !

Thanks for the quick replies and help !
Paweld's suggestion with the Synapse variant worked perfectly !
Synapse code is paired with ssl3 libraries under Lazarus ...

I still have a problem with the Indy variant which is under Delphi 7 (Indy 10.6 I think), here it is still paired with the old ssl1.1 libraries,
it is likely that Thaddy is right and it needs to be updated to ssl3 ...

I have a problem with this, that in the eInvoice system the Delphi 7 and Indy pair works perfectly, I don't quite understand how it wont works with the eDespatchAdvice system? If I update to ssl3 then what will happen to eInvoice, do you think that what worked with ssl1 will work with ssl3 ?
« Last Edit: January 01, 2026, 06:21:14 pm by patyit »

paweld

  • Hero Member
  • *****
  • Posts: 1568
Re: Pleas help, Synapse / Indy HttpSend problem
« Reply #5 on: January 01, 2026, 07:34:31 pm »
I don't know if it's an OpenSSL issue, but in the case of Indy and TIdMultipartFormDataStream, redundant headers (transfer encoding, content type) are added to the request content, and the API may not recognize them. Check by manually generating the request content:
Code: Pascal  [Select][+][-]
  1. uses
  2.   IdHTTP, IdSSLOpenSSL;
  3.  
  4. const
  5.   CRLF = #13#10;
  6.  
  7. procedure TForm1.Button1Click(Sender: TObject);
  8. var
  9.   sl: TStringList;
  10. begin
  11.   sl := TStringList.Create;
  12.   if PostXmlFile('https://api.demoeotpremnica.mfin.gov.rs/public/documents/requests', 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx', '20250310-001', 'd:\test.xml', sl) then
  13.     ShowMessage('Response:' + CRLF + sl.Text)
  14.   else
  15.     ShowMessage('Error:' + CRLF + sl.Text);
  16.   sl.Free;
  17. end;
  18.  
  19. function TForm1.PostXmlFile(url, apikey, requestid, xmlfilename: String; var outdata: TStringList): Boolean;
  20. var
  21.   hc: TIdHTTP;
  22.   hcssl : TIdSSLIOHandlerSocketOpenSSL;
  23.   b, s: String;
  24.   sl: TStringList;
  25.   msin, msout: TMemoryStream;
  26. begin
  27.   outdata.Clear;
  28.   Result := False;
  29.   if not FileExists(xmlfilename) then
  30.     raise Exception.Create('File not exists!');
  31.   sl := TStringList.Create;
  32.   sl.LoadFromFile(xmlfilename);
  33.   b := IntToHex(Random(MaxInt), 8);
  34.   //IdOpenSSLSetLibPath(IPat); // path to ssl libs
  35.   msin := TMemoryStream.Create;
  36.   msout := TMemoryStream.Create;
  37.   hc := TIdHTTP.Create;
  38.   hcssl := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
  39.   try
  40.     hcssl.SSLOptions.Mode := sslmClient;
  41.     hcssl.SSLOptions.SSLVersions := [sslvTLSv1_1,sslvTLSv1_2];
  42.     hc.IOHandler := hcssl;
  43.     //request body
  44.     s := '--' + b + CRLF;
  45.     s := s + 'Content-Disposition: form-data; name="RequestId"' + CRLF + CRLF + requestid + CRLF;
  46.     s := s + '--' + b + CRLF;
  47.     s := s + 'Content-Disposition: form-data; name="File"; filename="' + xmlfilename +'"' + CRLF;
  48.     s := s + 'Content-Type: text/xml' + CRLF + CRLF;
  49.     s := s + sl.Text;
  50.     s := s + CRLF + '--' + b + '--' + CRLF;
  51.     msin.Write(s[1], Length(s));
  52.     msin.Position := 0;
  53.     //
  54.     hc.HandleRedirects := True;
  55.     hc.ReadTimeout     := 30000;
  56.     hc.Request.Accept  := '*/*';
  57.     hc.Request.ContentType := 'multipart/form-data; boundary=' + b;
  58.     hc.Request.CustomHeaders.AddValue('Api-key', 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx');
  59.     hc.HTTPOptions := hc.HTTPOptions + [hoNoProtocolErrorException];
  60.     hc.Post(url, msin, msout);
  61.     msout.Position := 0;
  62.     Result := (hc.ResponseCode div 100) = 2;
  63.     outdata.LoadFromStream(msout);
  64.     if not Result then
  65.       outdata.Text := Format('[%d] - %s > %s', [hc.ResponseCode, hc.ResponseText, outdata.Text]);
  66.   finally
  67.     hcssl.Free;
  68.     hc.Free;
  69.     sl.Free;
  70.     msin.Free;
  71.     msout.Free;
  72.   end;
  73. end;

You can also use Indy with the latest version of SSL: https://forum.lazarus.freepascal.org/index.php/topic,69748.msg542222.html#msg542222
« Last Edit: January 01, 2026, 07:40:07 pm by paweld »
Best regards / Pozdrawiam
paweld

patyit

  • New Member
  • *
  • Posts: 33
Re: Pleas help, Synapse / Indy HttpSend problem
« Reply #6 on: January 01, 2026, 08:59:10 pm »
Hi Paweld !

Unfortunately this conversion did not help, the operation still ends with "Error 405 Not Allowed".
Now it's almost certain, should I try SSL3 ?!

Anyway, thanks for the help, I learned something again !

rvk

  • Hero Member
  • *****
  • Posts: 6939
Re: Pleas help, Synapse / Indy HttpSend problem
« Reply #7 on: January 01, 2026, 09:39:23 pm »
Code: Pascal  [Select][+][-]
  1.     Htt.MimeType := 'multipart/form-data; boundary='+bou;
  2.     Htt.Headers.Add('Content-Type: multipart/form-data');
  3.     Htt.Headers.Add('accept: */*');
  4.     Htt.Headers.Add('Api-key: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx');  //
  5.  
     
I don't do "indy". But for Synapse you have Htt.MimeType (which adds Content-Type) AND a manually added header for Content-Type (both with multipart).

In this case, you manually added Content-Type doesn't include the boundary, so the server doesn't see any of the fields given to it (hence the many errors about missing fields).

Remove the manually added ( Add('Content-Type') ) and see if it works better.

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1572
    • Lebeau Software
Re: Pleas help, Synapse / Indy HttpSend problem
« Reply #8 on: January 01, 2026, 11:43:03 pm »
in the case of Indy and TIdMultipartFormDataStream, redundant headers (transfer encoding, content type) are added to the request content, and the API may not recognize them.

You can disable that behavior by setting the TIdFormDataField.ContentType and TIdFormDataField.ContentTransfer properties to blank strings.

Unfortunately this conversion did not help, the operation still ends with "Error 405 Not Allowed".

I would suggest attaching a TIdLogFile component to the TIdHTTP.Intercept property, and look at the server's actual response. I'm guessing that you are being redirected to another URL, and that URL doesn't accept POST'ed data. That could explain why you are getting a 405 error.
« Last Edit: January 01, 2026, 11:47:21 pm by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

paweld

  • Hero Member
  • *****
  • Posts: 1568
Re: Pleas help, Synapse / Indy HttpSend problem
« Reply #9 on: January 02, 2026, 06:43:04 am »
You can disable that behavior by setting the TIdFormDataField.ContentType and TIdFormDataField.ContentTransfer properties to blank strings.
Hi Remy,
The ContentType property cannot be empty: https://github.com/IndySockets/Indy/blob/master/Lib/Protocols/IdMultipartFormData.pas#L844-L852
Best regards / Pozdrawiam
paweld

patyit

  • New Member
  • *
  • Posts: 33
Re: Pleas help, Synapse / Indy HttpSend problem
« Reply #10 on: January 03, 2026, 06:51:18 pm »
Hello everyone !
Linux (XUbuntu 24.04 x64) with Lazarus, Synapse package, OpenSSL3 everything works !

In the past few days I have been doing tests to see if it is worth converting a relatively large Delphi7 ERP application that is on WinXP with Delphi 7 32bit to Lazarus, all of this is installed in VitualBox ...

I suspect that the source of the problems is WinXP/VirtualBox, which works with OpenSSL1 (except for the new eDespatchAdvice system), but nothing works with OpenSSL3 !

I installed OpenSSL3 32bit (first v.3.5.4, then v.3.6) on WinXP, I have the appropriate DLLs,
(libssl-3.dll, libcrypto-3.dll) I copied these into the program's execution directory (just like I did before in the case of OpenSSL1 ssleay32.dll, libeay32.dll bitness is 32), but nothing !

And it looks like there's no way to install Indy OpenSSL3 support on Delphi 7 (by MWASoftware)... but that's no problem if Synapse works !

Based on the above description, any comforting ideas ?
« Last Edit: January 03, 2026, 06:53:35 pm by patyit »

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1572
    • Lebeau Software
Re: Pleas help, Synapse / Indy HttpSend problem
« Reply #11 on: January 05, 2026, 07:21:37 pm »
And it looks like there's no way to install Indy OpenSSL3 support on Delphi 7 (by MWASoftware)...

Indy itself still supports Delphi 7, but I can't attest to any of the 3rd party OpenSSL3 addons, like MWASoftware's IndySecOpenSSL, or JPeterMugaas's TaurusTLS. Any issues you have with them will have to be taken up with their respective authors.
« Last Edit: January 09, 2026, 05:18:18 am by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

rvk

  • Hero Member
  • *****
  • Posts: 6939
Re: Pleas help, Synapse / Indy HttpSend problem
« Reply #12 on: January 06, 2026, 11:26:14 am »
... but that's no problem if Synapse works !
Does Synapse work for you now on XP and OpenSSL3?

If not... you do know that you need to use ssl_openssl3 in your uses clause and it's best to take the latest source from https://github.com/geby/synapse.

If it still doesn't work... write a clear post about what is not working and what you did to try to get it to work.

patyit

  • New Member
  • *
  • Posts: 33
Re: Pleas help, Synapse / Indy HttpSend problem
« Reply #13 on: January 21, 2026, 10:25:29 am »
Greetings to all !

Thank you everyone for your help, the problem was caused by my own mistake and not by the OpenSSL libraries, Synapse or the Indy package !
What happened was that I misspelled the URL address, one letter was entered incorrectly, which I recorded in the program configuration,
so it was not obvious ...

Thank you again for the helpfulness from everyone who commented on the topic, and sorry for my own stupidity !  :-[

 

TinyPortal © 2005-2018