Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Customize content-type header for text/html/app #2819

Closed
anselmh opened this issue Dec 2, 2015 · 1 comment
Closed

Customize content-type header for text/html/app #2819

anselmh opened this issue Dec 2, 2015 · 1 comment
Assignees

Comments

@anselmh
Copy link

anselmh commented Dec 2, 2015

Hey,

I’m trying to set up an application that sets the content-type response header dynamically. The goal is to set a charset in the URL (as parameter) and use that value in the header as charset. It’s an application to test a 3rd party integration in various charset / encoding environments.

I tried to set the content type via
res.set('content-type': 'text/html; charset=mycharset');
which does not work.

I then got the advice to set it in my res.render method but it seems utf-8 is hardcoded and does not accept any overwrites.

Is it simply not possible? If so, may I ask why that is? If there is a solution, can you help me out?

@dougwilson
Copy link
Contributor

Hi! If you are giving Express a string, Express has to encode that sting into bytes somehow. Express does this by using the universal UTF8 encoding format.

If you are using a different charset, then you're going to have to encode your string into bytes using that charset first and hand Express a Buffer object containing those bytes. Then, Express will not touch the charset, because it did not perform the encoding.

Example:

res.type('text/html; charset=foobar');
res.send(new Buffer('05f2bac793a1', 'hex'))

You'll see that we send your bytes as-is and also your charset as-is.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants