Martin Fischer
Martin Fischer

Reputation: 697

Haskell and the web (UTF-8)

I have a Haskell-Script what putStr some stuff ... and this output I want to display on a webpage. I compiled the Haskellscript and let it be executed like a CGI Script, works well.

But when I have special characters in the putStr, it will fail:

Terminal-Output:

<p class="text">what happrns at ä ?</p>

Web output:

what happrns at 

And nothing behind it is displayed ... wha happened?

Upvotes: 1

Views: 139

Answers (2)

jamshidh
jamshidh

Reputation: 12070

Assuming that the Haskell program is properly outputting utf-8, you will still need to let the browser know what encoding you are using. In HTML 4, you can do this as follows

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

(add this inside the <head> tag).

In HTML 5 you can use

<meta charset="UTF-8">

You can verify that the Haskell program is outputting the correct format using xxd on the command line

./myProgram | xxd

This will show you the actual output in bytes, so that you can verify on a character by character basis.

Upvotes: 1

ErikR
ErikR

Reputation: 52039

Some questions:

  1. What Content-type header is your CGI script sending?

  2. Are you setting the encoding on the stdout handle?

You should always send a Content-type header, and in that header you can stipulate the character encoding. See this page for more details.

Also, the encoding for Haskell file handles is determined by your OS's current default. It might be UTF-8, but it also might be something else. See this SO answer for more info.

So to make sure that the characters you send appear correctly on a web page, I would:

  1. Send a Content-type header with charset=utf8
  2. Make sure the encoding for stdout is also set to utf8:

    import System.IO
    ...
    hSetEncoding stdout utf8
    

If the Content-type header and your output encoding agree, things should work out.

Upvotes: 3

Related Questions