Recent

Author Topic: Post data from fpc like submitting a web form using Indy10?  (Read 1185 times)

BosseB

  • Sr. Member
  • ****
  • Posts: 468
Post data from fpc like submitting a web form using Indy10?
« on: December 20, 2023, 06:28:44 pm »
I need to automate a logging function from an FPC/Lazarus program on Windows.
It currently runs on Windows Server 16.

Current situation:
My application was written back in 2018 in order to send emails on activity events.
It was built with the then current versions of Fpc and Lazarus including Indy10.
And it uses Indy to send the the emails using an account on my ISP.

The purpose of emailing is as a log transfer/sharing function but it has been difficult maintaining the email connections when services become more and more locked down on the Internet...

New transfer method
So I want to switch away from emailing and instead send a POST packet to a php script on my web-server just like if I had filled in a form online with a number of text fields as parameters including a multi-line log text in html format.

I currently have 7 text fields for parameters (they include spaces and special chars like / etc).
Additionally there is one log text which is multi-line and is in HTML format.

My existing program builds the log message from incoming data and sends it as an email message with the html text as the body and I would like to keep the extraction functions in place and just fork out when the emailing is invoked such that I can call another function for this new messaging path.

I can easily build a web page with a form where the submit action is a POST to the php script and it will receive the data and can act on it.
However, this is only a development option for the php script since the logging is from an automated unattended service.

Questions:
1) How can I send a call to the php script just like if it came from a submitted form?

2) How do I send multi-line html text compared to single line text strings?

3) Can I use Indy10 since that is already integrated into the remote system application?

Grateful for any insights/suggestions/weblinks on this issue!
--
Bo Berglund
Sweden

delphius

  • Jr. Member
  • **
  • Posts: 68
Re: Post data from fpc like submitting a web form using Indy10?
« Reply #1 on: December 20, 2023, 07:59:35 pm »
The purpose of emailing is as a log transfer/sharing function but it has been difficult maintaining the email connections when services become more and more locked down on the Internet...

If something works well, then just don't touch it.

What exactly is the problem with sending logs via email right now?

Why was it decided to reinvent the wheel, and not just finish sending by mail?
fpmtls - ssl/tls 1.3 implementation in pure pascal
fpmailsend - sending a simple email message
pascal-webui - use web browser as gui and fpc as backend

BosseB

  • Sr. Member
  • ****
  • Posts: 468
Re: Post data from fpc like submitting a web form using Indy10?
« Reply #2 on: December 20, 2023, 11:01:21 pm »
The purpose of emailing is as a log transfer/sharing function but it has been difficult maintaining the email connections when services become more and more locked down on the Internet...

If something works well, then just don't touch it.
Well the whole point is that it is NOT working well. Lately several disruption events...
Quote
What exactly is the problem with sending logs via email right now?
The emails don't reach their destinations...
Quote
Why was it decided to reinvent the wheel, and not just finish sending by mail?
We have experienced several sudden stops in email delivery caused by mail servers like GMail.
Those previous times the emails were server side stuffed into a spam folder for unknown reasons and every user was having to reconfigure GMail to allow these emails to pass through.
Now, after the last stop it is no longer the case that they go into spam, they just disappear...

Being fed up by this I am trying to use another route by letting the mail server on the web-server ISP do the sending via a local php script....

And before I set out to create that script I want to check that I can actually feed it the data, which is extracted by the existing application.
Hence the question on how to POST stuff like this using Indy10 like a web browser would when the user hits Submit.

Also I have stated POST rather than GET in order to protect the actual command from spoofing.
I have other scripts on the web server which sends report emails but these use GET and the info is minimal and can be sent using a simple URL with the added data.
I rather want to use POST but don't know how.
--
Bo Berglund
Sweden

Leledumbo

  • Hero Member
  • *****
  • Posts: 8760
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Post data from fpc like submitting a web form using Indy10?
« Reply #3 on: December 21, 2023, 07:43:26 am »
1) How can I send a call to the php script just like if it came from a submitted form?
Just send a HTTP POST of enctype application/x-www-form-urlencoded.
2) How do I send multi-line html text compared to single line text strings?
To HTTP, both are the same, just encode line endings properly.
3) Can I use Indy10 since that is already integrated into the remote system application?
Now since this thing can be considered ancient, it depends. AFAIK it's capable of sending HTTP POST, but lacking in the secure version (SSL / TLS). So, if your PHP side is protected, the request might not come through.

For a reference, this is how you would do it with fphttpclient:
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}{$H+}
  2.  
  3. uses
  4.   Classes,
  5.   SysUtils,
  6.   sslsockets,
  7.   fphttpclient;
  8.  
  9. var
  10.   LReqBody: TStrings;
  11.   LResBody: TStringStream;
  12. begin
  13.   with TFPHTTPClient.Create(nil) do
  14.     try
  15.       LReqBody := TStringList.Create;
  16.       with LReqBody do begin
  17.         // both fieldN and valueN will be urlencode-d automatically since we call FormPost with TStrings as the request body
  18.         Values['field1'] := 'value1';
  19.         Values['field2'] := 'value2';
  20.         Values['field3'] := 'value3';
  21.       end;
  22.       LResBody := TStringStream.Create('');
  23.       FormPost('https://whatever.com/asyouwish',LReqBody,LResBody);
  24.       WriteLn('Response: ' + LResBody.DataString);
  25.     finally
  26.       LResBody.Free;
  27.       LReqBody.Free;
  28.       Free;
  29.     end;
  30. end.
  31.  

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1322
    • Lebeau Software
Re: Post data from fpc like submitting a web form using Indy10?
« Reply #4 on: December 21, 2023, 06:16:48 pm »
1) How can I send a call to the php script just like if it came from a submitted form?

You can use the TIdHTTP.Post() method with a TStringList holding name=value pairs. That will send an HTTP request in the standard application/x-www-form-urlencoded format, which PHP can read using $_POST, eg:

Code: Pascal  [Select][+][-]
  1. var
  2.   PostData: TStringList;
  3. begin
  4.   PostData := TStringList.Create;
  5.   try
  6.     PostData.Add('To=' + ...);
  7.     PostData.Add('From=' + ...);
  8.     PostData.Add('Message=' + ...);
  9.     IdHTTP1.Post(url, PostData);
  10.   finally
  11.     PostData.Free;
  12.   end;
  13. end;
  14.  

2) How do I send multi-line html text compared to single line text strings?

The exact same way as above.  Simply put the entire multi-line text into the TStringList as a single string, including any embedded line breaks.  They will be encoded accordingly.

3) Can I use Indy10 since that is already integrated into the remote system application?

Yes.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1322
    • Lebeau Software
Re: Post data from fpc like submitting a web form using Indy10?
« Reply #5 on: December 21, 2023, 06:22:41 pm »
3) Can I use Indy10 since that is already integrated into the remote system application?
Now since this thing can be considered ancient, it depends.

Wow :o

AFAIK it's capable of sending HTTP POST, but lacking in the secure version (SSL / TLS).

That is a misleading statement.  Indy absolutely supports sending HTTPS over SSL/TLS, and has for many many years.  It just takes a couple of steps to configure (ie, attach an SSLIOHandler component, etc), as SSL/TLS is not built-in to the TIdHTTP component itself - and for good reason, as Indy's SSL/TLS subsystem is actually modular so you can plug in whatever security library you want.  It is built around OpenSSL by default, but you are not limited to just OpenSSL.

So, if your PHP side is protected, the request might not come through.

Oh, that is just BS.  Of course it will come through when using Indy.  Don't scare the OP with false claims.
« Last Edit: December 21, 2023, 06:24:50 pm by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

 

TinyPortal © 2005-2018