我已经使用django活塞实现了API,其中从sms/mms
获取数据。对于MMS案例,我必须使用图像和其他人发布XML数据。这是我在handlers.py上的代码片段
def create(self, request,*args,**kwagrs):
try:
file_type = None
raw_data = request.raw_post_data
data = serializers.deserialize("xml", raw_data)
try:
parser = Parse(data.stream.getvalue())
message = parser.get_message()
action_id = parser.get_action_id()
except Exception,e:
return HttpResponse(Response({'sender':parser.get_sender(),'error_description':str(e)}).get_error_response(), mimetype='text/xml')
if action_id in ['o','m','vt','vh','yritys']:
return self.post_message(request,parser)
elif action_id == 'poista' or action_id == 'lopeta':
return self.expired_message(request,parser)
elif action_id == 'tiedot':
return self.get_contact_info(request,parser)
except Exception,e:
ad_id = None
return HttpResponse(Response({'sender':parser.get_sender(),'error_description':str(e)}).get_error_response(), mimetype='text/xml')
当我发布xml data
CURL
时,Firefox
工作,但当我使用httprequester
,"BAD REQUEST"
时我会{{1}}
答案 0 :(得分:1)
检查一下: I get a 400 Bad Request error while using django-piston
创建中间件:
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
在设置中添加:
MIDDLEWARE_CLASSES =( 'app.middleware.ContentTypeMiddleware', )
答案 1 :(得分:0)
尝试hurl.it进行API测试。检查您的帖子数据。如果需要,设置标题信息。