django - HTTPS PUT Request using djangorestframework -


i trying send https put request restful api django web service using djangorestframework (drf: http://django-rest-framework.org/) view. cannot work due django's cross site request forgery (csrf) protection.

the put request intended allow unauthenticated users add resource.

what have considered/tried:

  1. disabling csrf -- not acceptable. api runs on same django instance non-api service. disabling csrf protection risk.
  2. using x-requested-with: xmlhttprequest header on put request (i control clients). doesn't work -- still csrf error.
  3. using @crsf_exempt decorator on put view. if -- framework defines class, not view.

my current best option write put views myself without using drf's view class. can use @crsf_exempt decorator successfully.

i'd use drf's view class -- cannot see how. can you?

thanks james cran wellward, able solve issue using method_decorator.

class exampleview(responsemixin,view): renderers=default_renderers def get(self,request): response=response(200,{'msg':'called via get'}) return self.render(response) def post(self,request): response=response(200,{'msg':'called via post'}) return self.render(response) @method_decorator(csrf_exempt): def dispatch(self,*args,**kwargs): return super(eampleview,self).dispatch(*args,**kwargs) 

and test it:

curl -x http://www.example.com/rest/exampleview/ 

returns:

{msg: 'called via get'} 

and

curl -x post http://www.example.com/rest/exampleview/ 

returns:

{msg: 'called via post'} 

hth. see original post.


Comments

Popular posts from this blog

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

php - Get uncommon values from two or more arrays -

Adding duplicate array rows in Php -