I'm using class-based views.
class UserCreate(View):
def post(self, request):
data = request.data.get
social_id = data('social_id')
social_source = data('social_source')
user = User(social_id=social_id, social_source=social_source, access_token=access_token)
user.save()
return JsonResponse({'response':200})
Whenever I post data on this URL, it says CSRF token missing or incorrect.
curl -X POST --header "Content-Type: application/json" --header "Accept: application/json" -d "{
\"social_id\": \"string\",
\"social_source\": \"FB/Gmail\",
\"access_token\": \"string\"
}" "http://127.0.0.1:8000/users/"
I've had this problem while getting data from the form in function views. There I used to add @csrf_exempt on my view and it would work. When I added @csrf_exempt to my post
method, it does not work. How can I post the data?
This is because is class_based views you need to decorate
the dispatch method
for csrf_exempt to work
class UserCreate(View):
@method_decorator(csrf_exempt)
def dispatch(self, request, *args, **kwargs):
return super(UserCreate, self).dispatch(request, *args, **kwargs)
def post():
....