7

Before asking this question I've checked other posts on the similar. The answers provided simply suppressed the warning, instead of proposing a remedy.

I have a web application written with Python + Flask, which handles async requests sent with jquery in the unicode format. Content-Type:application/json; charset=UTF-8

Here is an example of the data sent from my web browser to the server. The data can be a mix of latin and non-latin characters. I've noticed that non-latin characters are codified but latin are not.

body: "Хочу Ñходить на #фильм Картель. ÐеÑÐ¼Ð¾Ñ‚Ñ€Ñ Ð½Ð° поÑредÑтвенные отзывы, фильм должен быть прикольным. Вот его опиÑание http://www.allocine.fr/film/fichefilm_gen_cfilm=202971.html"

Here is the definition of my MySqlAlchemy class

class Post(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    body = db.Column(db.Unicode(1024))

Now, when I try to submit the data to the database I receive the error below:

SAWarning: Unicode type received non-unicode bind para m value.
param.append(processorskey)

In between I'm using Flask-Restful component to handle HTTP requests:

class PostListApi(Resource):
    decorators = [login_required]
    def post(self):
        body=request.json['body']
        post = Post(body=body)
        db.session.add(post)
        db.session.commit()

The question is how should I handle the data correctly on the server side thus to make sure that non-unicode characters are not being written into the DB?

3
  • It's ok that ascii is left as is, ASCII text are valid UTF-8-encoded Unicode as well.. How do you handle your data inbetween? Commented Nov 20, 2013 at 10:15
  • alko: I've updated to the post Commented Nov 20, 2013 at 12:50
  • What python version are you using? There's a big difference in how Unicode is handled between Python 2 and 3! Commented Oct 1, 2015 at 7:09

1 Answer 1

3

You can simply force the body to unicode.

class PostListApi(Resource):
    decorators = [login_required]
    def post(self):
        body=request.json['body']
        post = Post(body=unicode(body))
        db.session.add(post)
        db.session.commit()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.