Forum > Databases

I want something to receive HTTP POSTs from another device

(1/1)

Sheepdog:
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.)

dje:
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:
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:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program VerySimpleHttpServer; uses SysUtils, fphttpserver; type   TMySimpleServer = class(TFPHttpServer)  public    procedure HandleRequest(var ARequest: TFPHTTPConnectionRequest; var AResponse: TFPHTTPConnectionResponse); override;  end;   procedure ShowMe(const AName, AValue: string);  begin    WriteLn(AName, ': ', AValue);  end;   procedure TMySimpleServer.HandleRequest(var ARequest: TFPHTTPConnectionRequest; var AResponse: TFPHTTPConnectionResponse);  begin    inherited;    ShowMe('Method', ARequest.Method);    ShowMe('UserAgent', ARequest.UserAgent);    ShowMe('PathInfo', ARequest.PathInfo);    ShowMe('Query', ARequest.Query);    ShowMe('URL', ARequest.URL);    if Assigned(ARequest.QueryFields) then begin      ShowMe('QueryFields', ARequest.QueryFields.CommaText);    end;    if Assigned(ARequest.ContentFields) then begin      ShowMe('ContentFields', ARequest.ContentFields.CommaText);    end;    with AResponse.Contents do begin      Add('<HTML><BODY>');      Add('<H1>Hello, World!<H1>');      Add('</BODY></HTML>');      AResponse.ContentType := 'text/html';    end;  end; begin  with TMySimpleServer.Create(nil) do begin    Port := 1234;    WriteLn('Start HTTP Server');    Active := True;  end;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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Start HTTP ServerMethod: GETUserAgent: Mozilla/5.0 (X11; Linux armv7l; rv:91.0) Gecko/20100101 Firefox/91.0PathInfo: Query: date=27/8/22&time=9:40&temp=coldURL: /?date=27/8/22&time=9:40&temp=coldQueryFields: date=27/8/22,time=9:40,temp=coldContentFields: Method: GETUserAgent: Mozilla/5.0 (X11; Linux armv7l; rv:91.0) Gecko/20100101 Firefox/91.0PathInfo: /favicon.icoQuery: URL: /favicon.icoQueryFields: 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.

Navigation

[0] Message Index

Go to full version