iPhone POSTing to Django and gets CSRF verification failed

折月煮酒 提交于 2019-12-03 09:41:18
jro

Normally this boils down to setting the headers correctly. There is an answer that already details this. The relevant part you need is this:

xhr.setRequestHeader("X-CSRFToken", token)

See the linked answer for details on getting the token from the cookies, for brevity I didn't copy it from there. I don't really know the context of your code, so this method of retrieval might not directly apply. Regardless, you need to get the token somehow.

When you have the token, add a header to the NSMutableURLRequest. Upon posting the request, the error should be gone.

[request addValue:token forHTTPHeaderField:@"X-CSRFToken"];

Am I wrong or it just don't make sense to use this on native app?

In that case, you could just disable this protection using this decorator:

from django.views.decorators.csrf import csrf_exempt
@csrf_exempt
  def view_without_csrf_protection(request):
    pass
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!