title: Getting Binary HTTP Post parameter in an Java/Tomcat/HttpServlet
body: <p>I have a binary value being URL Encoded, and then POSTed to an HttpServlet. The following code shows how I first attempted to extract this data. Very simple except that the result is a String, not bytes.</p>
  
  <p>This seemed to work at first, except that an extra byte appeared three bytes from the end. What I eventually figured out was that my data was being treated as Unicode and converted from one Unicode encoding to UTF-8.</p>
  
  <p>So, other that getting the entire post body and parsing it myself, how can I extract my data without treating it as a string after the url encoding is decoded? Have I misunderstood the specs for posted data in general, or is this a Java/Tomcat specific issue?</p>
  
  <pre><code>protected void doPost(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {
  
      // Receive/Parse the request
      String requestStr = request.getParameter("request");
      byte[] rawRequestMsg = requestStr.getBytes();
  </code></pre>
  
  <p>Here is a snippet of the Python test script I'm using for the request:</p>
  
  <pre><code>    urlRequest = urllib.urlencode( {'request': rawRequest} )
  
      connection = urllib.urlopen(self.url, data = urlRequest)
      result = connection.readlines()
      connection.close()
  </code></pre>