zin*_*non 5 python django curl
我正在尝试使用postman. 我的应用程序是django 1.11.6使用python 3.5.
我的应用程序安装在ubuntu服务器上。我没有创建csrf令牌的登录机制。
这些是我遵循的步骤:
cURL命令。我的curl命令是:
curl -i -H 'Accept: application/json; indent=4' -X POST https://127.0.0.1/users/:register/ -d "id=111&firstname=zinonas&yearofbirth=2007&lastname=Antoniou&othernames="
Run Code Online (Sandbox Code Playgroud)
我得到的错误是Forbidden (403) - CSRF verification failed. Request aborted。
当我运行curl commandvia 时cygwin,它工作正常。
这是我正在使用的视图函数:
class ApiUserRegister(APIView):
permission_classes = ()
serializer_class = RegisterUserSerializer
def post(self, request):
serializer = RegisterUserSerializer(data=request.data)
# Check format and unique constraint
serializer.is_valid(raise_exception=True)
data = serializer.data
if User.objects.filter(id=data['id']).exists():
user = User.objects.get(id=data['id'])
is_new = "false"
resp_status = status.HTTP_200_OK
else:
user = User.objects.create(id=data['id'],
firstname=data['firstname'],
yearofbirth=data['yearofbirth'],
lastname=data['lastname'],
othernames=data['othernames'])
user.save()
is_new = "true"
resp_status = status.HTTP_201_CREATED
resp = {"user": serializer.get_serialized(user),
"isnew": is_new}
return Response(resp, status=resp_status)
Run Code Online (Sandbox Code Playgroud)
在settings.py我有:
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated',
),
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.SessionAuthentication',
'rest_framework.authentication.TokenAuthentication',
'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
)
}
Run Code Online (Sandbox Code Playgroud)
Try this.
from django.views.decorators.csrf import csrf_exempt
class ApiUserRegister(APIView):
permission_classes = ()
serializer_class = RegisterUserSerializer
@csrf_exempt
def post(self, request):
serializer = RegisterUserSerializer(data=request.data)
Run Code Online (Sandbox Code Playgroud)
将你的课程更新为这样
from braces.views import CsrfExemptMixin
class your_class(CsrfExemptMixin, ......yours_here)
def post(...):
[....]
Run Code Online (Sandbox Code Playgroud)
这将告诉 django 允许没有 csrf 的请求
| 归档时间: |
|
| 查看次数: |
9296 次 |
| 最近记录: |