I have a Flask app with a route defined like this:
@app.route("/api/<value>")
def foo(value):
...
On my local machine, using Flask development server, everything works correctly.
But on production, where the app is deployed on IIS using wfastcgi
, I get a 404 error when name
contains non-ascii characters. In IIS logs the route seems OK (shown on the log file as UTF-8) but on the Flask side the request is received with gibberish characters. The following method:
@app.before_request
def log_request():
req: flask.Request = request
logger.info(
f"{req.method} {req.path}}"
)
prints this to the log:
GET /api/�
This is more than just a display issue. The value itself is incorrect and I can't use it to process the request correctly.
I tried to use the quote
and unquote
methods from the urllib
package but it does not seem to solve the problem.
Is there any setting on IIS I can use to fix this?
(IIS 10, Flask 1.1.2)
I found the following on Microsoft's documentation for PHP with FastCGI. I worked for me with Flask/wfastcgi as well:
By default, the FastCGI extension uses ASCII encoding when setting server variables that are used by PHP. When the requested URL contains non-ASCII characters, server variables that derive their values from the requested URL string may be set incorrectly. PHP applications that rely on those server variables may not work as a result.
To prevent this, the FastCGI extension can be configured to use UTF-8 encoding when setting server variables. To configure FastCGI to use UTF-8 encoding for a particular set of server variables, use the REG_MULTI_SZ registry key FastCGIUtf8ServerVariables and set its value to a list of server variable names. For example:
reg add HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\w3svc\Parameters /v FastCGIUtf8ServerVariables /t REG_MULTI_SZ /d REQUEST_URI\0PATH_INFO
The above example configures the FastCGI extension to use UTF-8 encoding when setting the REQUEST_URI and PATH_INFO server variables.
After setting the registry key, restart IIS by using the iisreset command.