paul
paul

Reputation: 51

HTTP POST with XML data does not work in Django-Piston

I have implemented API with django piston in which its take data from sms/mms . For MMS case i have to post XML data with image and others . Here is my code snippet on 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')

when I am posting xml data with CURL its working , but when i use Firefox, httprequester its throwing me "BAD REQUEST"

Upvotes: 0

Views: 548

Answers (2)

anils
anils

Reputation: 1962

Check this: I get a 400 Bad Request error while using django-piston

Create middleware as:

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

Add it in settings:

MIDDLEWARE_CLASSES = ( 'app.middleware.ContentTypeMiddleware', )

Upvotes: 1

Tauquir
Tauquir

Reputation: 6913

Try hurl.it for API testing. Check your post data. Set your header info if required.

Upvotes: 0

Related Questions