2011-12-26 13 views
9

Sto costruendo un'applicazione RESTful su Django e Tasty Pie. Ho un problema con i metodi PUT e POST. Quando faccio una richiesta tramite Curl, ho un errore.Django Tasty Pie: serializzazione su metodi PUT e POST

richieste Curl:

curl -i -H "Content-Type :application/json" -X POST -d '{"site_id":2,"post":2}' --user log:pass http://domain.com/core/api/v1/bookmarklet_post/ 

curl -i -H "Content-Type :application/json" -X PUT -d '{"site_id":2,"post":2}' --user log:pass http://domain.com/core/api/v1/bookmarklet_post/ 

l'errore:

{"error_message": "The format indicated 'application/x-www-form-urlencoded' had no 
available deserialization method. Please check your ``formats`` and ``content_types`` on 
your Serializer.", "traceback": "Traceback (most recent call last):\n\n File  
\"/home/ilya/envs/rebelmouse/lib/python2.7/site-packages/tastypie/resources.py\", line 
178, in wrapper\n response = callback(request, *args, **kwargs)\n\n File 
\"/home/ilya/envs/rebelmouse/lib/python2.7/site-packages/tastypie/resources.py\", line 
379, in dispatch_list\n return self.dispatch('list', request, **kwargs)\n\n File 
\"/home/ilya/envs/rebelmouse/lib/python2.7/site-packages/tastypie/resources.py\", line 
409, in dispatch\n response = method(request, **kwargs)\n\n File 
\"/home/ilya/envs/rebelmouse/lib/python2.7/site-packages/tastypie/resources.py\", line 
1020, in put_list\n deserialized = self.deserialize(request, request.raw_post_data, 
format=request.META.get('CONTENT_TYPE', 'application/json'))\n\n File 
\"/home/ilya/envs/rebelmouse/lib/python2.7/site-packages/tastypie/resources.py\", line 
328, in deserialize\n deserialized = self._meta.serializer.deserialize(data,  
format=request.META.get('CONTENT_TYPE', 'application/json'))\n\n File 
\"/home/ilya/envs/rebelmouse/lib/python2.7/site-packages/tastypie/serializers.py\", line 
159, in deserialize\n raise UnsupportedFormat(\"The format indicated '%s' had no 
available deserialization method. Please check your ``formats`` and ``content_types`` on 
your Serializer.\" % format)\n\nUnsupportedFormat: The format indicated 'application/x- 
www- form-urlencoded' had no available deserialization method. Please check your 
``formats`` and ``content_types`` on your Serializer.\n"} 

Serializer sono implementate nella mia classe di risorse:

class BookmarkletPostResource(Resource): 
    site_id = fields.CharField(attribute='site_id') 
    post = fields.CharField(attribute='post')  

    class Meta: 
     resource_name = 'bookmarklet_post' 
     include_resource_uri = False 
     limit = 10 
     default_format = "application/json" 
     object_class = ProxyStore 
     authorization = Authorization() 
     serializer = Serializer() 

risposta

18

il tipo di contenuto i due punti di intestazione sono fuori luogo.

Esso dovrebbe essere simile a questo, invece: -H "Content-Type: application/json"

+0

E come si fa a fare con jQuery? 'ContentType: 'application/json'' non funziona ... – Zulu

+0

usa il tipo: opzione" json "quando si effettua la richiesta. c'è anche un metodo .json se ricordo bene – jujule