通过API创建用户的邮递员帖子会导致CSRF错误-为什么?

时间:2018-07-12 11:03:21

标签: django django-rest-framework postman

我目前正在尝试Postman。

我目前有一个视图,基本上这样通过REST调用创建用户 我正在为此使用django rest

class CreateEmployer_CreateAPIView(CreateAPIView):
serializer_class = Serializer_CreateEmployer_RX
permission_classes = (permissions.AllowAny,)

def post(self, request, format=None):
    user = request.user
    serializer = Serializer_CreateEmployer_RX(data=request.data)
    if serializer.is_valid():
        Savedmodel = serializer.save()
        return Response(serializer.data, status=status.HTTP_201_CREATED)
    return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

这在urls.py中

url(r"^create/$", csrf_exempt(CreateEmployer_CreateAPIView.as_view()),name="Create"),

当我尝试通过邮递员调用此api时收到错误消息

Forbidden (403)
CSRF verification failed. Request aborted.
You are seeing this message because this site requires a CSRF cookie when submitting forms. This cookie is required for security reasons, to ensure that your browser is not being hijacked by third parties.

我的问题是为什么我会收到此错误,我该如何解决?据我所知,CSRF是在将表单加载到网页中时,将带有CSRF值的cookie与表单一起发送,然后在提交表单时将CSRF令牌的值进行比较,以确保它是通过以下方式发送的:现在,在我的情况下,没有表单,并且此视图上的身份验证为AllowAny。这也曾经工作过,但现在突然我收到了这个错误。我的问题是我该如何解决?由于没有表单,如何在这种情况下获取CSRF令牌值?我看过很多有关此问题和邮递员的帖子,但是据我了解,其中大多数建议在标题中添加X-CSRFToken。即使我添加了这些,我如何获得这些值?

enter image description here

0 个答案:

没有答案