Recent

Author Topic: [Solved]Garbled web page content for Asian characters with fpWeb  (Read 1252 times)

PeterHu

  • Jr. Member
  • **
  • Posts: 57
Following below tutorial:
https://wiki.freepascal.org/fpWeb_Tutorial

I made a simple http web page,it works as the tutorial guides.

But when I changed below:
Code: [Select]

procedure TFPWebModule1.DataModuleRequest(Sender: TObject; ARequest: TRequest;
AResponse: TResponse; var Handled: Boolean);
begin
  AResponse.Content := 'Hello, World!';
  Handled := true;
end;

to

Code: [Select]

{$mode objfpc}{$H+}   {$codepage utf8}

interface

uses
  sysutils, classes, httpdefs, fphttp, fpweb,
  lazutf8,lazutf16,windows;             

...

procedure tfpwebmodule1.datamodulerequest(sender: tobject; arequest: trequest; aresponse: tresponse;
  var handled: boolean);
var
  //content:ansiString; //-->tried
  content:string;
begin
  //setTextCodePage(Output,CP_UTF8);//--->tried
  Content:='Hello, world!' +sLineBreak+
'¡Hola Mundo!'+sLineBreak+
'Γειά σου Κόσμε!'+sLineBreak+
'Привет, мир!'+sLineBreak+
'こんにちは世界!'+sLineBreak+
'你好世界!'+sLineBreak+
'नमस्ते दु)निया!'+sLineBreak+
'👋🌎!';

  //aresponse.Content:=unicodestring(content);//-->tried
  aResponse.Content:=content;
  handled:=true;
end;                           

The webpage content are garbled as show in the screenshot.

I have added {$codepage utf8} and added latUtf8 in the uses clause.

So what was I doing wrong here and how to resolve this issue?

Help would be appreciated.

« Last Edit: July 12, 2025, 08:35:35 am by PeterHu »

Nimbus

  • Jr. Member
  • **
  • Posts: 69
Re: Garbled web page content for Asian characters with fpWeb
« Reply #1 on: July 12, 2025, 08:27:33 am »
Hi,

What is happening is just the browser ignoring the line breaks because it threats the contents as HTML markup. You did not specify the Content-type response header, so it is sent as `text/html` by default, and line break characters (CR, LF) are ignored in the HTML. Also, it need's to know the contents is in the utf-8 from the same header.

If you want to use the pain text, you need to say it in the header, by adding
Code: Pascal  [Select][+][-]
  1. AResponse.ContentType := 'text/plain; charset=utf-8';

Or if you want the HTML, then you can use <br> tags to insert line breaks.
Code: Pascal  [Select][+][-]
  1. AResponse.Content := 'Good day.<br>Добрий день.';
  2. AResponse.ContentType := 'text/html; charset=utf-8';
  3.  

Hope that helps.
« Last Edit: July 12, 2025, 08:29:36 am by Nimbus »

PeterHu

  • Jr. Member
  • **
  • Posts: 57
Re: Garbled web page content for Asian characters with fpWeb
« Reply #2 on: July 12, 2025, 08:34:38 am »
Hi,

What is happening is just the browser ignoring the line breaks because it threats the contents as HTML markup. You did not specify the Content-type response header, so it is sent as `text/html` by default, and line break characters (CR, LF) are ignored in the HTML. Also, it need's to know the contents is in the utf-8 from the same header.

If you want to use the pain text, you need to say it in the header, by adding
Code: Pascal  [Select][+][-]
  1. AResponse.ContentType := 'text/plain; charset=utf-8';

Or if you want the HTML, then you can use <br> tags to insert line breaks.
Code: Pascal  [Select][+][-]
  1. AResponse.Content := 'Good day.<br>Добрий день.';
  2. AResponse.ContentType := 'text/html; charset=utf-8';
  3.  

Hope that helps.

This really helps,thank you!

 

TinyPortal © 2005-2018