ash84
ash84

Reputation: 817

Change flask.app.request_class in flask according to sending request

From client send data(name field encoed 'euc-kr'), POST http://127.0.0.1, name=테스트&charset=euc-kr

In server(based on flask) receive data, but only Unicode and broken display.

@app.route('/', methods='post'):
def post():
     print request.charset #utf-8
     print request.url_charset #utf-8

     print type(request.form['name']) #unicode

So I use the subclass of Flask.Requsest class for supporting charset:

# main.py 
class EuckrRequest(Request):
    url_charset = 'euc-kr'
    charset = 'euc-kr'

app = Flask(__name__, static_url_path='', static_folder='static')
app.request_class=EuckrRequest

So good and not broken display. But I want to change app.request_class according to charset in POST data.

How to modify code? app.request_context?, app.before_request?

Upvotes: 0

Views: 1164

Answers (2)

ash84
ash84

Reputation: 817

I solve use request.get_data()

   import urllib
   urllib.unquote(request.get_data()).decode('euc-kr')

Upvotes: 0

user78110
user78110

Reputation:

dynamically changing the request class is the wrong way to go about this, since the request class is instantiated very early to begin with

i suggest you reach in to request.environ and handle the details explicitly from the data that came in

Upvotes: 1

Related Questions