Author Topic: fpWeb "Hello World!" fails  (Read 402 times)

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4744
  • I like bugs.
fpWeb "Hello World!" fails
« on: August 31, 2026, 06:08:33 pm »
Now I must ask a newbie question: What is wrong with my "Hello World!" web app?
The project is attached. I made it pretty much like instructed here:
 https://wiki.lazarus.freepascal.org/fpWeb_Tutorial

URL: http://localhost:8080/
says
 localhost:8080 sent an error: 400 Bad Request
If I change the port to something else, is says it cannot connect to server. So the original URL communicates with the server but gets an error message in return.
I understood no other server app is needed for this to work. Is it so?

This is surprising. Years ago I have done some web programming with Java and PHP and didn't have problems with a "Hello World!". Did I experiment with fpWeb earlier? I honestly don't remember.

My system: Manjaro Linux, Lazarus trunk and FPC trunk.
Thus the weblaz package is from Lazarus trunk. First I had FPC 3.2.2 and got the same error. Switching to FPC trunk didn't help.
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

dseligo

  • Hero Member
  • *****
  • Posts: 1704
Re: fpWeb "Hello World!" fails
« Reply #1 on: August 31, 2026, 06:44:02 pm »
fpWeb_tutorial says that TSimpleFileModule is used to send files.
Look here for webserver: https://wiki.lazarus.freepascal.org/fpWeb_Tutorial#Webserver_example

olatov

  • Full Member
  • ***
  • Posts: 112
Re: fpWeb "Hello World!" fails
« Reply #2 on: August 31, 2026, 06:54:45 pm »
Perhaps this part

Quote
Routing

Since FPC 3.0.4, a new routing mechanism has been implemented. Instead of maintaining backward compatibility, it is decided that the new routing will be the default. Thus, any old code  (or new code depending on old routing) must be ported by adding:

Application.LegacyRouting := true;

in the .lpr.

For a fresh project, I'd imagine the new routing (httproute unit) would be a better option. With it, a hello world would be very simple, no need in a dedicated module

Code: Pascal  [Select][+][-]
  1. program WebHello;
  2.  
  3. uses
  4.   fphttpapp, HTTPDefs, httproute;
  5.  
  6. procedure HandleHome(Request: TRequest; Response: TResponse);
  7. begin
  8.   Response.Content := 'Hello world!';
  9. end;
  10.  
  11. begin
  12.   HTTPRouter.RegisterRoute('/', @HandleHome, True);
  13.  
  14.   Application.Port := 9999;
  15.   Application.Initialize;
  16.   Application.Run;
  17. end.


ALLIGATOR

  • Sr. Member
  • ****
  • Posts: 476
  • FPC git[main] Lazarus git[main] 💪🐯💪
Re: fpWeb "Hello World!" fails
« Reply #3 on: August 31, 2026, 07:06:21 pm »
What is wrong with my "Hello World!" web app?

Code: Pascal  [Select][+][-]
  1. http://127.0.0.1:8080/TFPWebModule1

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4744
  • I like bugs.
Re: fpWeb "Hello World!" fails
« Reply #4 on: August 31, 2026, 07:35:06 pm »
Code: Pascal  [Select][+][-]
  1. http://127.0.0.1:8080/TFPWebModule1
Oops, yes, that works! The tutorial was a little wrong there. Thanks!

OK, I must experiment with the httproute, too. No legacy routing stuff is needed.
When I make a real app, it should be modular but that can be done with the httproute system, too. It seems to simplify things.
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4744
  • I like bugs.
Re: fpWeb "Hello World!" fails
« Reply #5 on: August 31, 2026, 08:22:44 pm »
fpWeb_tutorial says that TSimpleFileModule is used to send files.
Look here for webserver: https://wiki.lazarus.freepascal.org/fpWeb_Tutorial#Webserver_example
Yes, your link shows the httproute way of things. BTW, I did not use TSimpleFileModule in my experiment.

The tutorial is a typical Wiki page, meaning it mixes old and new stuff. Nobody dared to remove confusing old instructions. They should be removed especially from a "Hello World" tutorial, as "Hello World" means the most simple and easy beginner program for a certain language / environment.
Old historical systems can be placed on a separate page if needed.
Many Wiki pages have the same problem, sadly.
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

Thaddy

  • Hero Member
  • *****
  • Posts: 19805
  • Glad to be alive.
Re: fpWeb "Hello World!" fails
« Reply #6 on: September 01, 2026, 06:37:54 am »
@Nimbus

Amazingly simple working example. Thanks.
Any "programmer" that knows only one programming language is not a programmer

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4744
  • I like bugs.
Re: fpWeb "Hello World!" fails
« Reply #7 on: September 01, 2026, 09:47:23 am »
fpWeb_tutorial says that TSimpleFileModule is used to send files.
Look here for webserver: https://wiki.lazarus.freepascal.org/fpWeb_Tutorial#Webserver_example
Yes, your link shows the httproute way of things. BTW, I did not use TSimpleFileModule in my experiment.
Sorry, actually the wizard had added TSimpleFileModule in the .lpr file for some reason. It only calls a class procedure and sets a class variable, thus no objects are created which is OK.

Yes, the httproute system shown by @Nimbus should be the way to go for new users.
However the "HTTP Server Application" wizard does not generate such code. It should be improved. At the bottom it has "Web module to create:". The first option could be "No module, use httproute" or something like that.

The wiki page should be updated, too. The "Hello World" is done in an outdated way (and its URL doesn't work). Somebody with better knowledge please update it.
I can look at the wizard. I guess Michael Van Canneyt is busy and will not fix it anytime soon.

BTW, how to extend a httproute app? If I only serve HTML pages, do I need to create any modules at all? I will go through the examples ...
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

Thaddy

  • Hero Member
  • *****
  • Posts: 19805
  • Glad to be alive.
Re: fpWeb "Hello World!" fails
« Reply #8 on: September 01, 2026, 09:51:31 am »
BTW, how to extend a httproute app? If I only serve HTML pages, do I need to create any modules at all? I will go through the examples ...
Just register multiple routes. Example on its way.
Code: Pascal  [Select][+][-]
  1. program WebHello2;
  2.  
  3. uses
  4.   fphttpapp, HTTPDefs, httproute;
  5.  
  6. procedure HandleHome(Request: TRequest; Response: TResponse);
  7. begin
  8.   Response.Content := 'Hello World';
  9. end;
  10.  
  11. procedure HandleHomeOther(Request: TRequest; Response: TResponse);
  12. begin
  13.   Response.Content := 'This is a webserver fully written in Freepascal';
  14. end;
  15.  
  16. begin
  17.   HTTPRouter.RegisterRoute('/', @HandleHome, True);  // default
  18.   HTTPRouter.RegisterRoute('/other', @HandleHomeOther, false); // alternative
  19.  
  20.   Application.Port := 80;
  21.   Application.Initialize;
  22.   Application.Run;
  23. end.
That's all  8) Note I tested the real port 80 here on a real webserver too, not just localhost.
The handler can also point to more complex things like a CGI, as long as the handler is implemented correctly. It only depends on the path you specify. Very powerfull.
Want a more complex example?
« Last Edit: September 01, 2026, 10:22:57 am by Thaddy »
Any "programmer" that knows only one programming language is not a programmer

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4744
  • I like bugs.
Re: fpWeb "Hello World!" fails
« Reply #9 on: Today at 10:52:32 am »
Thanks Thaddy but I must first study some online material.

BTW, I improved the wizard in commit 5dc5d3bd0 already at 2. of September. Forgot to mention.
Now it supports the httproute thing about like I envisioned earlier. Please test. Does it make any sense?
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

 

TinyPortal © 2005-2018