Http Server lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- michoux · 2022-12-05 10:54 Hello I created a new HTTP Server Application in Lazarus on port 8080 When ever I try to reach it on localhost:8080  from browser I get following erro ************************************++ The application encountered the following error:     Error: Not found     Stack trace:     $00438D3C     $0043873C     $00438D9E     $0043234F     $00432A47     $0042BCA5     $00436047     $004359FF     $00435B84     $004219F1     $00402B7F     $7730FEF9     $778C7BBE     $778C7B8E ******************************* ---------------------------------------------------------------------- michoux · 2022-12-05 11:31 Somehow now server is working I have following code, it is from tutorial of fpweb Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure JSON(req: TRequest; res: TResponse);var  n: Integer;  f: TUploadedFile;  i: Integer;begin  n := req.Files.Count;  if n = 0 then    with res.Contents do    begin      Add('
');      Add('');      Add('');      Add('');      Add('
');    end  else  begin    f := req.Files[0];    res.Contents.LoadFromStream(f.Stream);  end; end;                                     I want to change this so that I receive file directly  with post For example with curl. c:\curl\bin\curl.exe -X GET --data-binary @c:\test.xml http://localhost:9080 Regards Mich ---------------------------------------------------------------------- michoux · 2022-12-05 11:37 Hi, Ok I found out all is written in request. just a simple code will output the file content posted with curl. c:\curl\bin\curl.exe -X GET --data-binary @c:\live_timing\race.xml http://localhost:9080/ Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure JSON(req: TRequest; res: TResponse);var  n: Integer;  f: TUploadedFile;  i: Integer;  s:string;begin  s := req.Content; writeln(s);  end; ---------------------------------------------------------------------- michoux · 2022-12-05 12:15 Now I have next question. I make a form and I start a web server with a button. form1.FPHttpServer2.Active:=true This blocks my app totally. ---------------------------------------------------------------------- PierceNg · 2022-12-05 12:36 Quote from: michoux on December 05, 2022, 01:15:46 pmNow I have next question. I make a form and I start a web server with a button. form1.FPHttpServer2.Active:=true This blocks my app totally. Run the HTTP server on a thread. Something like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  TWebServerThread = class(TThread)    protected      procedure Execute; override;    public      constructor Create(CreateSuspended: boolean);  end; constructor TWebServerThread.Create(CreateSuspended: boolean);begin  inherited Create(CreateSuspended);  FreeOnTerminate := true;end; procedure TWebServerThread.Execute;begin  fphttpapp.Application.Run;end; ---------------------------------------------------------------------- michoux · 2022-12-05 12:59 Thanks. How do I call TWebServerThread.Execute from my app? ---------------------------------------------------------------------- PierceNg · 2022-12-05 13:11 Quote from: michoux on December 05, 2022, 01:59:15 pmHow do I call TWebServerThread.Execute from my app? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i begin  // Set up web server.  fphttpapp.Application.Port := 8000;  fphttpapp.Application.Threaded := true; // web server itself is multi- threaded  fphttpapp.Application.Initialize;  // Run the server in a thread.  // If you want to start the server on button press, then move next line to the button press handler.  TWebServerThread.Create(false); // false means the server thread runs immediately   // Now run LCL application.  RequireDerivedFormResource:=True;  Forms.Application.Scaled:=True;  Forms.Application.Initialize;  Forms.Application.CreateForm(TForm1, Form1);  Forms.Application.Run;   end. See https://wiki.freepascal.org/Multithreaded_Application_Tutorial for more info. ---------------------------------------------------------------------- michoux · 2022-12-05 14:42 Thank you very much. I inserted form on which I have a report with lazReport. After I get response from a webserver I want to call to open report, which is on form1 which I added. I use form1.frReport1.ShowReport; This kills my app with SIGSERV ... Is this related to thred? ---------------------------------------------------------------------- PierceNg · 2022-12-05 15:01 Quote from: michoux on December 05, 2022, 03:42:02 pmThank you very much. I inserted form on which I have a report with lazReport. After I get response from a webserver I want to call to open report, which is on form1 which I added. I use form1.frReport1.ShowReport; This kills my app with SIGSERV ... Is this related to thred? Dunno. If you post a simplified but complete project that compiles and demonstrates the crash, then others will be better placed to help. ---------------------------------------------------------------------- michoux · 2022-12-05 15:33 Here is simple code. I also have a form with lazreport on it this is all. I just want to open report with  form1.frReport1.LoadFromFile('test.lrf');     form1.frReport1.ShowReport; Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  program Project1; {$mode objfpc}{$H+} uses  {$IFDEF UNIX}{$IFDEF UseCThreads}  cthreads,  {$ENDIF}{$ENDIF}  Classes,SysUtils, fphttpapp, httpdefs, httproute, fpjson, jsonparser, interfaces,forms,Unit1; type  TWebServerThread = class(TThread)    protected      procedure Execute; override;    public      constructor Create(CreateSuspended: boolean);  end;   constructor TWebServerThread.Create(CreateSuspended: boolean);  begin    inherited Create(CreateSuspended);    FreeOnTerminate := true;  end;   procedure TWebServerThread.Execute;  begin    fphttpapp.Application.Run;  end; procedure test(req: TRequest; res: TResponse);var  s:string;  rawJson: AnsiString;  people: TJSONArray;  person: TJSONObject;  personEnum: TJSONEnum;begin  // Get the JSON data    rawJson:=req.Content;     writeln(rawjson);     form1.frReport1.LoadFromFile('test.lrf');    form1.frReport1.ShowReport;end;    begin    fphttpapp.Application.Port := 8000;  HTTPRouter.RegisterRoute('/test', @test, true);  fphttpapp.Application.Threaded := true;  fphttpapp.Application.Initialize;  TWebServerThread.Create(false);    RequireDerivedFormResource:=True;  Forms.Application.Scaled:=True;  Forms.Application.Initialize;  Forms.Application.CreateForm(TForm1, Form1);  Forms.Application.Run; end. ---------------------------------------------------------------------- PierceNg · 2022-12-05 15:46 Quote from: michoux on December 05, 2022, 04:33:44 pmHere is simple code. I also have a form with lazreport on it this is all. I just want to open report with  form1.frReport1.LoadFromFile('test.lrf');     form1.frReport1.ShowReport; Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  program Project1; {$mode objfpc}{$H+} uses  {$IFDEF UNIX}{$IFDEF UseCThreads}  cthreads,  {$ENDIF}{$ENDIF}  Classes,SysUtils, fphttpapp, httpdefs, httproute, fpjson, jsonparser, interfaces,forms,Unit1; type  TWebServerThread = class(TThread)    protected      procedure Execute; override;    public      constructor Create(CreateSuspended: boolean);  end;   constructor TWebServerThread.Create(CreateSuspended: boolean);  begin    inherited Create(CreateSuspended);    FreeOnTerminate := true;  end;   procedure TWebServerThread.Execute;  begin    fphttpapp.Application.Run;  end; procedure test(req: TRequest; res: TResponse);var  s:string;  rawJson: AnsiString;  people: TJSONArray;  person: TJSONObject;  personEnum: TJSONEnum;begin  // Get the JSON data    rawJson:=req.Content;     writeln(rawjson);     form1.frReport1.LoadFromFile('test.lrf');    form1.frReport1.ShowReport;end;    begin    fphttpapp.Application.Port := 8000;  HTTPRouter.RegisterRoute('/test', @test, true);  fphttpapp.Application.Threaded := true;  fphttpapp.Application.Initialize;  TWebServerThread.Create(false);    RequireDerivedFormResource:=True;  Forms.Application.Scaled:=True;  Forms.Application.Initialize;  Forms.Application.CreateForm(TForm1, Form1);  Forms.Application.Run; end.    According to the wiki page on multi-threaded programming I linked to earlier, section 'Lazarus Widgetset Interfaces': QuoteOnly one thread in an application should call LCL APIs, usually the main thread. Other threads can make use of the LCL through a number of indirect methods, ... So your '/test' web handler shouldn't do LCL GUI things directly. Instead, it should save the data received, and somehow inform the main thread to process the saved data. See the wiki page for ideas. ---------------------------------------------------------------------- egsuh · 2022-12-06 04:20 Hi michoux, I think here's an example you are looking for. https://www.freepascal.org/~michael/articles/webserver1/webserver1.pdf You can also find the sources in the following list. Scroll down the list until you find webserver. https://www.freepascal.org/~michael/articles/ ---------------------------------------------------------------------- michoux · 2022-12-06 10:01 Hi Thank you for your help. I managed to get things going. Now next step is to read some data and parse it. I got following script which sends some data to serve. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i                  

Localhost service call test

               

        Call        

     I get the data but it is is not in real json format it is some strange outpout. Here is the received data. "id=10&title=HP+Pavilion+15-DK1056WM&description=HP+Pavilion+15- DK1056WM+Gaming...&price=1099&discountPercentage=6.18&rating=4.43&stoc k=89&brand=HP+Pavilion&category=laptops&thumbnail=https%3A%2F%2Fi.dumm yjson.com%2Fdata%2Fproducts%2F10%2Fthumbnail.jpeg&images%5B%5D=https%3 A%2F%2Fi.dummyjson.com%2Fdata%2Fproducts%2F10%2F1.jpg&images%5B%5D=htt ps%3A%2F%2Fi.dummyjson.com%2Fdata%2Fproducts%2F10%2F2.jpg&images%5B%5D =https%3A%2F%2Fi.dummyjson.com%2Fdata%2Fproducts%2F10%2F3.jpg&images%5 B%5D=https%3A%2F%2Fi.dummyjson.com%2Fdata%2Fproducts%2F10%2Fthumbnail. jpeg" Regards Matija ---------------------------------------------------------------------- michoux · 2022-12-06 10:19 I think above is problem with ajax code posting Json, I will try to fix this