Rendering HTML does not have a default encoding
vinyll opened this issue Β· comments
The following code
class Index < Grip::Controllers::Http
def get(context : HTTP::Server::Context)
context
.html("<h1>Hello world π</h1>")
.halt
end
Should render <h1>Hello world π</h1>
Current rendering: <h1>Hello world Γ°οΏ½οΏ½οΏ½</h1>
In order to do so, the .html()
method could set the default encoding to utf-8
.
In the meantime, skipping the shortcut does the job:
class Index < Grip::Controllers::Http
def get(context : HTTP::Server::Context)
context
.put_resp_header("Content-Type", "text/html; charset=utf-8")
.send_resp("<h1>Hello world π</h1>")
.halt
end
While I'm preparing a PR, I'm wondering it UTF-8 should not be the default for other content types too
Also, I can't find test to adjust. Are there any somewhere?
/spec
is the place for tests. That was a noob question (which I am).
Hello,
You can send a PR for the html/1
method, something like this maybe?
def html(content, header = "text/html; charset=utf-8")
@response.headers.merge!({"Content-Type" => header})
@response.print(content)
self
end
So the html/1
method will still be available as html/2
for developers while giving you an optional argument and keeping the codebase intact.
@vinyll Can you provide same for other functions returning responses as well?
for text/1
, json/1
and binary/1
.
The binary/1
should look something like this:
def binary(content, header = "application/octet-stream")
@response.headers.merge!({"Content-Type" => header})
@response.print(content)
end
Hey, thanks for your support with the PR suggestion.
I know it would be faster to fix it yourself, and at the same time it's interesting for me to get a little task like this one.
I just released a PR but it's not considering the .binary()
method yet.
Having a look right away
I've updated the PR to consider the binary()
method.
Hello,
You can send a PR for the
html/1
method, something like this maybe?def html(content, header = "text/html; charset=utf-8") @response.headers.merge!({"Content-Type" => header}) @response.print(content) self endSo the
html/1
method will still be available ashtml/2
for developers while giving you an optional argument and keeping the codebase intact.
What do html/1 and html/2 mean?
It's an arity representation, the number after the / represents the argument count the function takes.
@vinyll I added you to the official Gitter chat of the framework, click here to view the chat.