[Solved]Garbled web page content for Asian characters with fpWeb
lazarus / 38-Networking and Web Programming
----------------------------------------------------------------------
PeterHu · 2025-07-12 03:16
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.
----------------------------------------------------------------------
olatov · 2025-07-12 06:27
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][+][-]window.onload = function(){var x1 =
document.getElementById("main_content_section"); if (x1) { var x =
document.getElementsByClassName("geshi");for (var i = 0; i
AResponse.ContentType := 'text/plain; charset=utf-8';
Or if you want the HTML, then you can use
tags to insert line
breaks.
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
AResponse.Content := 'Good day.
Добрий день.';AResponse.ContentType
:= 'text/html; charset=utf-8';
Hope that helps.
----------------------------------------------------------------------
PeterHu · 2025-07-12 06:34
Quote from: Nimbus on July 12, 2025, 08:27:33 amHi,
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][+][-]window.onload = function(){var x1 =
document.getElementById("main_content_section"); if (x1) { var x =
document.getElementsByClassName("geshi");for (var i = 0; i
AResponse.ContentType := 'text/plain; charset=utf-8';
Or if you want the HTML, then you can use
tags to insert line
breaks.
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
AResponse.Content := 'Good day.
Добрий день.';AResponse.ContentType
:= 'text/html; charset=utf-8';
Hope that helps.
This really helps,thank you!