I am using the django rest framework to perform API calls via IOS and I get the following error "CSRF Failed: CSRF cookie not set."
Here's my django API code:
class LoginView(APIView):
"""
List all snippets, or create a new snippet.
"""
@csrf_exempt
def get(self, request, format=None):
startups = Startup.objects.all()
serializer = StartupSerializer(startups, many=True)
return Response(serializer.data)
@csrf_exempt
def post(self, request, format=None):
profile = request.POST
....
What can I do?
If anyone is still following this question, the direct answer is that you need to use the decorator on the view method itself. The get
and post
methods defined on the APIView
class just tell DRF how the actual view should behave, but the view method that the django router expects is not actually instantiated until you call LoginView.as_view()
.
Thus, the solution is to add the csrf_exempt
decorator to urls.py
. It might look as follows:
#file: urls.py
from django.conf.urls import patterns, url
from django.views.decorators.csrf import csrf_exempt
import views
urlpatterns = patterns('',
url('^login/$', csrf_exempt(views.LoginView.as_view())),
...
)
However, as Mark points out above, csrf protection is important to prevent your sessions from being hijacked. I haven't worked with iOS myself, but I would look into using django's cookie-based csrf tokens. You can use the ensure_csrf_cookie
decorator to make django send a csrftoken
cookie with a response, and your POST
requests will validate as long as you include that token as an X-CSRFToken
header.
I've had the same issue. My problem was that I forgot to put .as_view()
in urls.py on MyAPIView
. So it have to be like:
url(r'$', GetLikesAPI.as_view(), name='list')
not:
url(r'$', GetLikesAPI, name='list')
The problem you encounter here is that django for processing your view is using whatever as_view()
method will return, not directly method get()
or post()
.
Therefore you should decorate your class-based view in one of following ways:
urlpatterns = patterns('', url('^login/$', csrf_exempt(views.LoginView.as_view())), ... )
dispatch()
method (pre django 1.9)from django.utils.decorators import method_decorator class LoginView(APIView): @method_decorator(csrf_exempt) def dispatch(self, *args, **kwargs): ...
from django.utils.decorators import method_decorator @method_decorator(csrf_exempt, name='dispatch') class LoginView(APIView): ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With