Tastypie deserializes the results in {"error": ""}

I am using tastypie with django. I have one line of code:

data = self.deserialize(request, request.body, format=request.META.get('CONTENT_TYPE', 'application/json'))

I use this code from the command line to send a mail request to my web server:

curl -X post -d "{ 'username' : 'user', 'password' : 'password' }" http://127.0.0.1:8000/api/employee/login/ --header "Content-Type:application/json"

When I run this, it leads to json response

{"error": ""}

Looking at the logs of my server, I see:

[15/Feb/2014 20:39:49] "post /api/user/login/ HTTP/1.1" 400 13

A log message recorded immediately before the deserialization string is successfully logged, but a log message recorded immediately after the deserialization string is not logged, so I am sure that the deserialization is incorrect. Does anyone know what might be wrong, or if I should consider something else as a problem?

+3
source share
1 answer

JSON . , . 400 ( ) . : {"username": "user", "password": "password"}. , " char CURL. Tastypie, , , , , API.

from tastypie.exceptions import BadRequest
from tastypie.serializers import Serializer
class VerboseSerializer(Serializer):
    """
    Gives message when loading JSON fails.
    """
    # Tastypie>=0.9.6,<=0.11.0
    def from_json(self, content):
        """
        Override method of `Serializer.from_json`. Adds exception message when loading JSON fails.
        """
        try:
            return json.loads(content)
        except ValueError as e:
            raise BadRequest(u"Incorrect JSON format: Reason: \"{}\" (See www.json.org for more info.)".format(e.message))

class MyResource(BaseModelResource):
    class Meta:
        serializer = VerboseSerializer(formats=['json'])
+5

All Articles