Tastypie with application/x-www-form-urlencoded

2024/9/22 6:57:02

I'm having a bit of difficulty figuring out what my next steps should be. I am using tastypie to create an API for my web application.

From another application, specifically ifbyphone.com, I am receiving a POST with no headers that looks something like this:

post data:http://myapp.com/api/
callerid=1&someid=2&number=3&result=Answered&phoneid=4

Now, I see in my server logs that this is hitting my server.But tastypie is complaining about the format of the POST.

{"error_message": "The format indicated'application/x-www-form-urlencoded' had no available deserializationmethod. Please check your formats and content_types on yourSerializer.", "traceback": "Traceback (most recent call last):\n\n File \"/usr/local/lib/python2.7/dist-packages/tastypie/resources.py\"

I also receive the same message when I try to POST raw data using curl, which I "believe" is the same basic process being used by ifbyphone's POST method:

curl -X POST --data 'callerid=1&someid=2&number=3&duration=4&phoneid=5' http://myapp.com/api/

So, assuming my problem is actually what is specified in the error message, and there is no deserialization method, how would I go about writing one?

#### Update ######

With some help from this commit ( https://github.com/toastdriven/django-tastypie/commit/7c5ea699ff6a5e8ba0788f23446fa3ac31f1b8bf ) I've been playing around with writing my own serializer, copying the basic framework from the documentation ( https://django-tastypie.readthedocs.org/en/latest/serialization.html#implementing-your-own-serializer )

import urlparse
from tastypie.serializers import Serializerclass urlencodeSerializer(Serializer):formats = ['json', 'jsonp', 'xml', 'yaml', 'html', 'plist', 'urlencode']content_types = {'json': 'application/json','jsonp': 'text/javascript','xml': 'application/xml','yaml': 'text/yaml','html': 'text/html','plist': 'application/x-plist','urlencode': 'application/x-www-form-urlencoded',}def from_urlencode(self, data,options=None):""" handles basic formencoded url posts """qs = dict((k, v if len(v)>1 else v[0] )for k, v in urlparse.parse_qs(data).iteritems())return qsdef to_urlencode(self,content): pass
Answer

This worked as expected when I edited my resource model to actually use the serializer class I created. This was not clear in the documentation.

class urlencodeSerializer(Serializer):formats = ['json', 'jsonp', 'xml', 'yaml', 'html', 'plist', 'urlencode']content_types = {'json': 'application/json','jsonp': 'text/javascript','xml': 'application/xml','yaml': 'text/yaml','html': 'text/html','plist': 'application/x-plist','urlencode': 'application/x-www-form-urlencoded',}def from_urlencode(self, data,options=None):""" handles basic formencoded url posts """qs = dict((k, v if len(v)>1 else v[0] )for k, v in urlparse.parse_qs(data).iteritems())return qsdef to_urlencode(self,content): passMyModelResource(ModelResoucre):class Meta:...serializer = urlencodeSerializer() # IMPORTANT
https://en.xdnf.cn/q/71978.html

Related Q&A

Check for areas that are too thin in an image

I am trying to validate black and white images (more of a clipart images - not photos) for an engraving machine. One of the major things I need to take into consideration is the size of areas (or width…

Sort Python Dictionary by Absolute Value of Values

Trying to build off of the advice on sorting a Python dictionary here, how would I go about printing a Python dictionary in sorted order based on the absolute value of the values?I have tried:sorted(m…

impyla hangs when connecting to HiveServer2

Im writing some ETL flows in Python that, for part of the process, use Hive. Clouderas impyla client, according to the documentation, works with both Impala and Hive.In my experience, the client worked…

django prevent delete of model instance

I have a models.Model subclass which represents a View on my mysql database (ie managed=False).However, when running my unit tests, I get:DatabaseError: (1288, The target table my_view_table of the DEL…

suppress/redirect stderr when calling python webrowser

I have a python program that opens several urls in seperate tabs in a new browser window, however when I run the program from the command line and open the browser using webbrowser.open_new(url)The std…

Bokeh logarithmic scale for Bar chart

I know that I can do logarithmic scales with bokeh using the plotting API:p = figure(tools="pan,box_zoom,reset,previewsave",y_axis_type="log", y_range=[0.001, 10**22], title="l…

Can I control the way the CountVectorizer vectorizes the corpus in scikit learn?

I am working with a CountVectorizer from scikit learn, and Im possibly attempting to do some things that the object was not made for...but Im not sure.In terms of getting counts for occurrence:vocabula…

mod_wsgi process getting killed and django stops working

I have mod_wsgi running in daemon mode on a custom Linux build. I havent included any number for processes or threads in the apache config. Here is my config:WSGIDaemonProcess django user=admin WSGIPro…

Reindex 2nd level in incomplete multi-level dataframe to be complete, inserting NANs on missing rows

I need to reindex the 2nd level of a pandas dataframe, so that the 2nd level becomes a (complete) list 0,...,(N-1) for each 1st level index.I tried using Allan/Haydens approach, but unfortunately it on…

ImportError: cannot import name _gdal_array from osgeo

I create a fresh environment, install numpy, then install GDAL. GDAL imports successfully and I can open images using gdal.Open(, but I get the ImportError: cannot import name _gdal_array from osgeo er…