title: Detecting the character encoding of an HTTP POST request
body: <p>I'm building a web service and have a node that accepts a POST to create a new resource.  The resource expects one of two content-types - an XML format I'll be defining, or form-encoded variables.</p>
  
  <p>The idea is that consuming applications can POST XML directly and benefit from better validation etc., but there's also an HTML interface that will POST the form-encoded stuff.  Obviously the XML format has a charset declaration, but I can't see how I detect the form's charset just from looking at the POST.</p>
  
  <p>A typical post to the form from Firefox looks like this:</p>
  
  <pre><code>POST /path HTTP/1.1
  Host: www.myhostname.com
  User-Agent: Mozilla/5.0 [...etc...]
  Accept: text/html,application/xhtml+xml, [...etc...]
  Accept-Language: en-gb,en;q=0.5
  Accept-Encoding: gzip,deflate
  Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
  Keep-Alive: 300
  Connection: keep-alive
  Content-Type: application/x-www-form-urlencoded
  Content-Length: 41
  
  field1=value1&amp;field2=value2&amp;field3=value3
  </code></pre>
  
  <p>Which doesn't seem to contain any useful indication of the character set.</p>
  
  <p>From what I can see, the application/x-www-form-urlencoded type is entirely defined in HTML, which just lays out the %-encoding rules, but doesn't say anything about what charset the data should be in.</p>
  
  <p>Basically, is there any way of telling the character set if I don't know the character set the HTML originally presented was?  Otherwise I'll have to try and guess the character set based on what chars are present, and that's always a bit iffy from what I can tell.</p>