title: node.js and utf-8 in POST data
body: <p>I am having problems decoding UTF-8 strings in POST data when using the Node.JS web server.</p>
  
  <p>See this complete testcase:</p>
  
  <pre><code>require("http").createServer(function(request, response) {
  
    if (request.method != "POST") {
  
      response.writeHead(200, {'Content-Type': 'text/html; charset=utf-8'});
      response.end('&lt;html&gt;'+
        '&lt;head&gt;&lt;meta http-equiv="content-type" content="text/html; charset=utf-8"&gt;&lt;/head&gt;'+
        '&lt;body&gt;'+
        '&lt;form method="post"&gt;'+
        '&lt;input name="test" value="Grüße!"&gt;&lt;input type="submit"&gt;'+
        '&lt;/form&gt;&lt;/body&gt;&lt;/html&gt;');
  
    } else {
  
      console.log("CONTENT TYPE=",request.headers['content-type']);
  
      var body="";
      request.on('data', function (data) {
        body += data;
      });
  
      request.on('end', function () {
        console.log("POST BODY=",body);
  
        response.writeHead(200, {'Content-Type': 'text/plain; charset=utf-8'});
        response.end("POST DATA:\n"+body+"\n---\nUNESCAPED:\n"+unescape(body)+
          "\n---\nHARDCODED: Grüße!");
      });
  
    }
  
  }).listen(11180);
  </code></pre>
  
  <p>This is a standalone web server that listens on port 11180 and sends a HTML page with a simple form that contains an input field with special characters. POSTing that form to the server will echo it's contents in a plain text response.</p>
  
  <p>My problem is that the special charactes are not being displayed properly neither on the console nor in the browser. This is what I see with both FireFox and IE:</p>
  
  <pre><code>POST DATA:
  test=Gr%C3%BC%C3%9Fe%21
  ---
  UNESCAPED:
  test=GrÃ¼Ãe!
  ---
  HARDCODED: Grüße!
  </code></pre>
  
  <p>The last line is a hardcoded string <code>Grüße!</code> that should match the value of the input field (as to verify that it's not a displaying problem). Obviously the POST data is not interpreted as UTF-8. The same problem happens when using <code>require('querystring')</code> to break the data into fields. </p>
  
  <p>Any clue?</p>
  
  <p><em>Using Node.JS v0.4.11 on Debian Linux 4, source code is saved in utf-8 charset</em></p>