0

I have this code:

var qs = require('querystring');
var http = require('http');

http.createServer(function (req, res) {

    if ('/' == req.url) {
        res.writeHead(200, { 'Content-Type': 'text/html'});
        res.end([
            '<form method="post" action="/url">',
            '<h1>My form</h1>',
            '<fieldset>',
            '<label>Personal information</label>',
            '<p>What is your name?</p>',
            '<input type="text" name="name">',
            '<p><button>Submit</button></p>',
            '</form>'
        ].join(''));
    } else if ('/url' == req.url && 'POST' == req.method) {
        var body = '';

        req.on('data', function (chunk) {
            body += chunk;
        });

        req.on('end', function () {
            res.writeHead(200, { 'Content-Type': 'text/html'});
            res.end('<p>Your name is: <strong>' + qs.parse(body).name + '</strong></p>');
        });
    }
}).listen(3000);

Lets say i write my swedish name "Anders Östman" name in the input field and POST the form. Everything seems to work fine, except that my name is output as Anders �stman. The charatcher "Ö" get trashed... I guess this have something to do with character encoding and I need to set/convert the parsed object to UTF-8.

Q: Is there a way to qs.parse() directly into a UTF-8 object? Or does object does'nt have encoding? Do i need to encode the object value when i output it instead?

3 Answers 3

1

To ensure the browser sends the form data properly encoded, try adding an explicit encoding header:

res.writeHead(200, { 'Content-Type': 'text/html; charset=UTF-8'});

To debug this, try adding

 console.log(body)

to the second branch. If it says

name=Anders+%D6stman

the browser is still sending Latin-1 (which is wrong). It should be

name=Anders+%C3%96stman
Sign up to request clarification or add additional context in comments.

8 Comments

Does'nt work. Neither does this: 'Content-Type': 'text/html; charset=utf-8'
@AndersÖstman: make sure to add the header to both parts and reload the server.
@AndersÖstman: try adding a console.log (see edit). What does it say?
@AndersÖstman: when you open the form page, what encoding does your browser indicate? (View Page Info in FF, Tools/Encoding in Chrome)? Have you restarted the browser?
@AndersÖstman: BTW, it should be "charset", not "encoding" - my bad.
|
1

You need to change the Content-Type for both the form and the POST response to text/html; charset=utf-8. After that you will see the expected result (I tested it locally):

var qs = require('querystring');
var http = require('http');

http.createServer(function (req, res) {

    if ('/' == req.url) {
        res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8'});
        res.end([
            '<form method="post" action="/url">',
            '<h1>My form</h1>',
            '<fieldset>',
            '<label>Personal information</label>',
            '<p>What is your name?</p>',
            '<input type="text" name="name">',
            '<p><button>Submit</button></p>',
            '</form>'
        ].join(''));
    } else if ('/url' == req.url && 'POST' == req.method) {
        var body = '';

        req.on('data', function (chunk) {
            body += chunk;
        });

        req.on('end', function () {
            res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8'});
            res.end('<p>Your name is: <strong>' + qs.parse(body).name + '</strong></p>');
        });
    }
}).listen(3000);

You could alternatively set the charset in the html as meta tags in the <head> with:

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

or for HTML5:

<meta charset="utf-8">

Comments

0

Seems like this was some problems in the cache or similiar... i tried in another browser, and it worked. Then i made a total cache deletion of chrome, and got it to work there aswell!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.