Recent

Author Topic: I want something to receive HTTP POSTs from another device  (Read 669 times)

Sheepdog

  • New Member
  • *
  • Posts: 39
I want something to receive HTTP POSTs from another device
« on: August 27, 2022, 09:23:44 am »
I think I can create an Arduino that will send HTTP "POST" messages from time to time. (When the POST should be sent would be up to the Arduino.)

I want to write something in Lazarus to run in a second computer. One sitting on my LAN, standing by to receive POST messages.

Let's say the content of the POST is "At 21:45 temperature was 12.6 C".

I want the Lazarus program to append that to a text file held on the hard drive of the computer the Lazarus program is running on.

(I can already write an app to add the contents of an edit box to a text file (and clear the edit box) every time a user typed something and then pressed Enter.)
« Last Edit: August 27, 2022, 12:02:57 pm by Sheepdog »

dje

  • Full Member
  • ***
  • Posts: 134
Re: I want something to receive HTML POSTs from another device
« Reply #1 on: August 27, 2022, 09:44:36 am »
You need TFPHTTPServer from the unit fphttpserver, which comes with FreePascal.
There is an example here:
https://github.com/alrieckert/freepascal/blob/master/packages/fcl-web/examples/httpserver/simplehttpserver.pas

Sheepdog

  • New Member
  • *
  • Posts: 39
Re: I want something to receive HTTP POSTs from another device
« Reply #2 on: August 27, 2022, 01:41:00 pm »
Many thanks, dje! You've taken me further down the road, I think.

If others reading this want to do what I want to do, on a Windows machine it SEEMS that you just rem out the wmecho library from the "Uses" clause. (That WILL fix some things. I don't know that it doesn't break others. I could find little on the web about wmecho... anyone with information would do us all a favor by posting it here?

I've put other notes about my "one-eyed-king-with a blindfold on" stmblings in the dark at...

https://trixiesgems.com/category/programming/lazarus/

dje

  • Full Member
  • ***
  • Posts: 134
Re: I want something to receive HTTP POSTs from another device
« Reply #3 on: August 27, 2022, 02:16:31 pm »
If you haven't written http servers before, I think its best to start with the simplest example, so you can see how this stuff actually works. It is kinda fun.
So, start with a simple console server:
Code: Pascal  [Select][+][-]
  1. program VerySimpleHttpServer;
  2.  
  3. uses SysUtils, fphttpserver;
  4.  
  5. type
  6.  
  7.   TMySimpleServer = class(TFPHttpServer)
  8.   public
  9.     procedure HandleRequest(var ARequest: TFPHTTPConnectionRequest; var AResponse: TFPHTTPConnectionResponse); override;
  10.   end;
  11.  
  12.   procedure ShowMe(const AName, AValue: string);
  13.   begin
  14.     WriteLn(AName, ': ', AValue);
  15.   end;
  16.  
  17.   procedure TMySimpleServer.HandleRequest(var ARequest: TFPHTTPConnectionRequest; var AResponse: TFPHTTPConnectionResponse);
  18.   begin
  19.     inherited;
  20.     ShowMe('Method', ARequest.Method);
  21.     ShowMe('UserAgent', ARequest.UserAgent);
  22.     ShowMe('PathInfo', ARequest.PathInfo);
  23.     ShowMe('Query', ARequest.Query);
  24.     ShowMe('URL', ARequest.URL);
  25.     if Assigned(ARequest.QueryFields) then begin
  26.       ShowMe('QueryFields', ARequest.QueryFields.CommaText);
  27.     end;
  28.     if Assigned(ARequest.ContentFields) then begin
  29.       ShowMe('ContentFields', ARequest.ContentFields.CommaText);
  30.     end;
  31.     with AResponse.Contents do begin
  32.       Add('<HTML><BODY>');
  33.       Add('<H1>Hello, World!<H1>');
  34.       Add('</BODY></HTML>');
  35.       AResponse.ContentType := 'text/html';
  36.     end;
  37.   end;
  38.  
  39. begin
  40.   with TMySimpleServer.Create(nil) do begin
  41.     Port := 1234;
  42.     WriteLn('Start HTTP Server');
  43.     Active := True;
  44.   end;
  45. end.
The server binds to port 1234, so run the server and pull up a browser and try these URL's
http://localhost:1234/
http://localhost:1234/?apple=orange&lazarus=great
http://localhost:1234/?date=27/8/22&time=9:40&temp=cold
An example output on the console is:
Code: Text  [Select][+][-]
  1. Start HTTP Server
  2. Method: GET
  3. UserAgent: Mozilla/5.0 (X11; Linux armv7l; rv:91.0) Gecko/20100101 Firefox/91.0
  4. PathInfo:
  5. Query: date=27/8/22&time=9:40&temp=cold
  6. URL: /?date=27/8/22&time=9:40&temp=cold
  7. QueryFields: date=27/8/22,time=9:40,temp=cold
  8. ContentFields:
  9. Method: GET
  10. UserAgent: Mozilla/5.0 (X11; Linux armv7l; rv:91.0) Gecko/20100101 Firefox/91.0
  11. PathInfo: /favicon.ico
  12. Query:
  13. URL: /favicon.ico
  14. QueryFields:
  15. ContentFields:

Its knda fun once you get started. Its just a matter of knowing what data is being sent and where it is stored. But... Adding this to a GUI is another story. The server needs to be threaded, and access to the TForm's controls _must_ be synchronized.

Have fun.

 

TinyPortal © 2005-2018