Search code examples
pythongoogle-app-engineunicodeutf-8marshmallow

Unable to load UTF-8 JSON data with marshmallow


I'm trying to use marshmallow to verify posted JSON data to my app. I post using Jquery like this:

var testdata = { "field1": "value1", "field2": "value2" };

$.ajax({
    type: "POST",
    url: "/api/v1/monitors",
    data: JSON.stringify(testdata),
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(data){alert(data);},
    failure: function(errMsg){alert(errMsg);}
});

On the server side I have a python app on Google App Engine with a schema like:

class TestSchema(Schema):
    field1 = fields.Str()
    field2 = fields.Str()

And a handler like:

def post(self):
    schema = TestSchema()
    result = schema.load(self.request.body)
    logging.error(result)

In the log I constantly get:

UnmarshalResult(data={}, errors={u'_schema': [u'Invalid input type.']})

But if I replace this line:

result = schema.load(self.request.body)

With this:

result = schema.load('{ "field1": u"value1", "field2": u"value2" }')

It works just fine, but I don't want to post unicode I want to use UTF-8. How can I get it to take the UTF-8 posted data and load it?


Solution

  • There was a missing s!

    The load JSON in marshmallow you need to use the .loads() and not the .load() function.

    Thank you for all your efforts to help!