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:
- disabling csrf -- not acceptable. api runs on same django instance non-api service. disabling csrf protection risk.
- using
x-requested-with: xmlhttprequest
header on put request (i control clients). doesn't work -- still csrf error. - 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
Post a Comment