title: Flask problems handling a request with URLencoded parameters
body: <p>I'm having a weird problem; I'm running Flask and I have an API function to cancel memberships. The URL also contains a parameter to set a reason for cancelation. this is a short text, and it can contain extended characters, so the string is URLencoded by the calling party.</p>
  
  <p>In certain cases however, Flask returns a 400 error, before it even reaches my own processing code. For example, the following URL:</p>
  
  <pre><code>curl -X "DELETE" "http://localhost:5000/contracts/C9ABA4AA-834E-4711-91A8-F21057DF693B?date=2015-8-1&amp;canceldate=2015-6-27&amp;booktoday=true&amp;overrideEnddate=true&amp;cancelreason=traslado+a+m%C3%A1s+de+15+km&amp;correctionreason="
  </code></pre>
  
  <p>Gives me a 400 error: <em>The browser (or proxy) sent a request that this server could not understand.</em> although it seems to be a perfectly valid URL.
  When I trap the error the underlying error data is: </p>
  
  <pre><code>'ascii' codec can't encode character u'\\xe1' in position 12: ordinal not in range(128)
  </code></pre>
  
  <p>I found out that all is well when I remove the encoded extended character <strong>á</strong>  (%C3%A1) from the URL string.</p>
  
  <p>I can also solve it by adding this workaround I found elsewhere to my app.<strong>init</strong>()</p>
  
  <pre><code>import sys
  reload(sys)
  sys.setdefaultencoding("utf-8")
  </code></pre>
  
  <p>I understood that this statement forces the Python interpreter to use UTF-8 by default to decode bytestrings, instead of ASCII.<br>
  This gives me a solution to my problem, but it feels overly complicated and unnessecary.  </p>
  
  <p>So, the real question is, what am I missing here? Is there a setting within Flask that I don't know about that solves this, or can it really be the case that Flask can't handle specific URL encoded strings?
  I would expect Flask to be able to handle URL encoded parameters by itself, without a workaround in non-library code...</p>
  
  <p>I have no traceback, because Flask will trap it and handle it as an HTTP error and return a 400 HTTP status code.</p>
  
  <p><strong>EDIT</strong>:
  The origin of the exception is in this part of the Flask code (site-packages\flask\app.py):</p>
  
  <pre><code>def full_dispatch_request(self):
      """Dispatches the request and on top of that performs request
      pre and postprocessing as well as HTTP exception catching and
      error handling.
  
      .. versionadded:: 0.7
  """
  self.try_trigger_before_first_request_functions()
  try:
      request_started.send(self)
      rv = self.preprocess_request()
      if rv is None:
          rv = self.dispatch_request()
  except Exception as e:
      rv = self.handle_user_exception(e)
  response = self.make_response(rv)
  response = self.process_response(response)
  request_finished.send(self, response=response)
  return response
  </code></pre>
  
  <p>The code skips to the except: and returns a 400 error.</p>
  
  <p>Thanks for any pointers you can give me.</p>