Search code examples
haskellhappstack

Why is Happstack (toResponse) using text/plain per default?


I'm using HStringTemplate to render a very simple template using a data structure to fill in the "holes". The result of the template rendering is just a String I fed toResponse with.

Even though this rendered template is valid html happstack uses text/plain for Content-Type.

What is the reason for this? Shouldn't text/html be default since it is a webserver? Do I really need to use toResponseBS and set text/html by myself?

Here is the code that creates the ServerPart Response

data Person = Person                                                                                                                                                                                                
    { name :: String                                                                                                                                                                                                
    , age ::Int                                                                                                                                                                                                     
    } deriving (Data, Typeable)                                                                                                                                                                                     

buildTemplate :: Person -> String -> FilePath -> ServerPart Response                                                                                                                                                
buildTemplate fields name template = do                                                                                                                                                                             
    unrendered <- liftIO $ readFile template                                                                                                                                                                        
    ok $ toResponse $ renderTemplate name fields unrendered                                                                                                                                                         

renderTemplate :: String -> Person -> String -> String                                                                                                                                                              
renderTemplate name fields unrendered = toString rendered                                                                                                                                                           
    where rendered = setAttribute name fields $ newSTMP unrendered

And here is the output from the webserver:

Head

Connection:Keep-Alive
Content-Type:text/plain; charset=UTF-8
Date:Wed, 09 Jan 2013 14:51:27 GMT
Server:Happstack/7.1.1
Transfer-Encoding:chunked

Body

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />
        <title>Memlikweb</title>
    </head>
    <body>
        <h1>Hello, Richard!<h1>
        <p>Do you have 25 for me?</p>
    </body>
</html>

Solution

  • If you pass Text.Html to toResponse the content type will be text/html. You are passing a string, which toResponse takes to mean that the content type is plain text.