1

I'm running a Flask web service, with text-input, but now I have the problem that the text-input sometimes consists of characters that are not included in the ASCII-character set (Example of error: "(Error: no text provided) 'ascii' codec can't encode character u'\u2019' in position 20)")

My code for the Flask web service looks (somewhat) like this:

class Classname(Resource):
    def __init__(self):
       self.reqparse = reqparse.RequestParser()
       self.reqparse.add_argument('text', type=str, required=True, help='Error: no text provided')
       super(Classname,self).__init__()

    def post(self):
       args = self.reqparse.parse_args()
       text = args['text']
       return resultOfSomeFunction(text)

I already tried to turning the ascii-string into unicode, but that didn't work (error: 'unicode' object is not callable). I also tried to add:

text = re.sub(r'[^\x00-\x7f]',r' ',text)

after the rule

text = args['text']

but that also gave me the same error ('ascii' codec can't encode character).

How can I solve this?

1 Answer 1

3

Have you tried removing type=str from self.reqparse.add_argument('text', type=str, required=True, help='Error: no text provided')?

Note:

The default argument type is a unicode string. This will be str in python3 and unicode in python2

Source: http://flask-restful-cn.readthedocs.org/en/0.3.4/reqparse.html

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! That was a rather simple solution, that I didn't see.
Happy to help. Free free to "accept" the answer if it works for you. Cheers.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.