I've met an odd issue related to the unicode in ajax requests to the Flask-Restful API server. The issue appears only on one machine, but not another.
I have a restful class. As you may note the characters fields are set to unicode.
class PostListApi(Resource):
def __init__(self):
self.reqparse = reqparse.RequestParser()
self.reqparse.add_argument('body', type = unicode, required = True, help = 'No description provided', location = 'json')
self.reqparse.add_argument('longitude', type = float, required = False, help = 'Unknown address', location = 'json')
self.reqparse.add_argument('latitude', type = float, required = False, help = 'Unknown address', location = 'json')
self.reqparse.add_argument('address', type = unicode, required = True, help = 'No address specified', location = 'json')
self.reqparse.add_argument('scheduled', type = str, required = True, help = 'Not scheduled correctly', location = 'json')
super(PostListApi, self).__init__()
My issue is that when I send an ajax request with latin characters in the BODY, the server replies with the 400 error.
Request URL:
Request Method:POST
Status Code:400 BAD REQUEST
Request Headersview source
Accept:undefined
Accept-Encoding:gzip,deflate,sdch
Accept-Language:fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4
Content-Length:159
**Content-Type:application/json; charset=UTF-8**
Proxy-Authorization:Basic a3lyeWxvLnlhdHNlbmtvOnBhc3N3b3JkMTIzNDU2
Proxy-Connection:keep-alive
User-Agent:Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36
X-Requested-With:XMLHttpRequest
Request Payloadview parsed
{"body":"this is a test","address":"141 Rue Saint-Martin, 75003 Paris, France","longitude":2.351530499999967,"latitude":48.8614575,"scheduled":"20131017 1000"}
But when the data is in cyrilic alphabet the server manages the request properly.
Request URL:
Request Method:POST
Status Code:500 INTERNAL SERVER ERROR
Request Headersview source
Accept:undefined
Accept-Encoding:gzip,deflate,sdch
Accept-Language:fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4
Content-Length:208
**Content-Type:application/json; charset=UTF-8**
Cookie:
Proxy-Authorization:Basic a3lyeWxvLnlhdHNlbmtvOnBhc3N3b3JkMTIzNDU2
Proxy-Connection:keep-alive
User-Agent:Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36
X-Requested-With:XMLHttpRequest
Request Payloadview source
{body:Йн незл ножтрюд оффÑндйт вÑш,…}
address: "Йн незл ножтрюд оффÑндйт вÑш"
body: "Йн незл ножтрюд оффÑндйт вÑш"
latitude: 48.8614575
longitude: 2.351530499999967
scheduled: "20131017 1000"
Response Headersview source
Connection:Keep-Alive
Content-Length:51
Content-Type:application/json
Date:Thu, 17 Oct 2013 08:03:41 GMT
Proxy-Connection:Keep-Alive
Server:WSGIServer/0.1 Python/2.7.5
Request generation is done with the ajax method of jquery
function ajaxRequest(uri, method, data){
var request = {
url: uri,
type: method,
contentType: "application/json; charset=utf-8",
accepts: "application/json",
cache: false,
dataType: 'json',
//data: data != null?((method == 'GET') ? $.param(data): utf8_encode(JSON.stringify(data))):null,
data: data != null?((method == 'GET') ? $.param(data): JSON.stringify(data)):null,
beforeSend: function (xhr) {},
error: function(jqXHR) {
console.log("ajax error " + jqXHR.status);
}
};
return $.ajax(request);
Have you met a similar issue before? How can this be tackled? I suppose that the latin characters must also be encoded to unicode before sending to the server. How could I force jquery to encode everything into unicode?
Thanks in advance!
I use follow code and I don't have this problem and problem with output
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
from flask import Flask
from flask.ext.restful import Api
from flask.ext.restful.representations.json import output_json
output_json.func_globals['settings'] = {'ensure_ascii': False, 'encoding': 'utf8'}
app = Flask(__name__)
api = Api(app)
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With