10đź‘Ť
This is happening because Piston doesn’t like the fact that ExtJS is putting “charset=UTF-8” in the content-type of the header.
Easily fixed by adding some middleware to make the content-type a bit more Piston friendly, create a file called middleware.py in your application base directory:
class ContentTypeMiddleware(object):
def process_request(self, request):
if request.META['CONTENT_TYPE'] == 'application/x-www-form-urlencoded; charset=UTF-8':
request.META['CONTENT_TYPE'] = 'application/x-www-form-urlencoded'
return None
Then simply include this middleware in your settings.py:
MIDDLEWARE_CLASSES = (
'appname.middleware.ContentTypeMiddleware',
)
7đź‘Ť
Proposed solutions still did not work for me (django 1.2.3/piston 0.2.2) so I’ve tweaked joekrell solution and this finally works (I’m only using POST and PUT, but presumably you can add other verbs to the list):
class ContentTypeMiddleware(object):
def process_request(self, request):
if request.method in ('POST', 'PUT'):
# dont break the multi-part headers !
if not 'boundary=' in request.META['CONTENT_TYPE']:
del request.META['CONTENT_TYPE']
with:
MIDDLEWARE_CLASSES = (
'appname.middleware.ContentTypeMiddleware',
)
I haven’t noticed any side-effect, but I can’t promise it’s bullet-proof.
- Automatically round Django's DecimalField according to the max_digits and decimal_places attributes before calling save()
- Programmatically sync the db in Django
4đź‘Ť
I have combined some of what other people have said, and added support for any content type, json for instance…
class ContentTypeMiddleware(object):
def process_request(self, request):
if request.method in ('POST', 'PUT') and request.META['CONTENT_TYPE'].count(";") > 0:
request.META['CONTENT_TYPE'] = [c.strip() for c in request.META['CONTENT_TYPE'].split(";") ][0]
return None
4đź‘Ť
I thought Eric’s solution worked the best but then ran into problems when saving things in admin. This tweak seems to fix it if anyone else comes across it:
class ContentTypeMiddleware(object):
def process_request(self, request):
if request.method in ('POST') and not 'boundary=' in request.META['CONTENT_TYPE']:
request.META['CONTENT_TYPE'] = [c.strip() for c in request.META['CONTENT_TYPE'].split(";") ][0]
return None
1đź‘Ť
In utils.py, change this.
def content_type(self):
"""
Returns the content type of the request in all cases where it is
different than a submitted form - application/x-www-form-urlencoded
"""
type_formencoded = "application/x-www-form-urlencoded"
ctype = self.request.META.get('CONTENT_TYPE', type_formencoded)
if ctype.strip().lower().find(type_formencoded) >= 0:
return None
return ctype
https://bitbucket.org/jespern/django-piston/issue/87/split-charset-encoding-form-content-type
1đź‘Ť
This is solution which worked for me, after a tweak:
class ContentTypeMiddleware(object):
def process_request(self, request):
if 'charset=UTF-8' in request.META['CONTENT_TYPE']:
request.META['CONTENT_TYPE'] = request.META['CONTENT_TYPE'].replace('; charset=UTF-8','')
return None
- Using Django ORM in threads and avoiding "too many clients" exception by using BoundedSemaphore
- Limit a single record in model for django app?
- Debugging slow Django Admin views
- Django: How can I find which of my models refer to a model
- Django OAuth Toolkit – Register a user
0đź‘Ť
We had a resource that was simply updating a timestamp based on the request credentials and PUT. It turns out that Piston does not like PUT without a payload. Adding an empty string payload ” fixed it.
A quick Google search indicates that other systems like Apache may not like PUT without a payload, as well.