How to make a POST simple JSON using Django REST Framework? CSRF token missing or incorrect

前端 未结 8 1972
清酒与你
清酒与你 2020-12-04 15:50

Would appreciate someone showing me how to make a simple POST request using JSON with Django REST framework. I do not see any examples of this in the tutorial anywhere?

相关标签:
8条回答
  • 2020-12-04 16:14

    the old Postman is having a problem with csrf tokens because it does not working with cookies.

    I suggest for you to switch to the new version of postman, it works with cookies and you will not face this problem again.

    0 讨论(0)
  • 2020-12-04 16:16

    OK, well now of course I take back what I said. CSRF does work as intended.

    I was making a POST request using a chrome plugin called POSTMAN. My POST request fails with CSRF enabled.

    But a curl POST request using

    curl -X POST -H "Content-Type: application/json" -d '
    {
        "name": "Manager",
        "description": "someone who manages"
    }' http://127.0.0.1:8000/lakeshoreProperties/roles/
    

    works fine... I had to take off the braces, i.e., [], and make sure there is a slash after the 's' in roles, i.e., roles/, and csrf enabled did not throw any errors.

    I'm not sure what the difference between calling using POSTMAN is vs using curl, but POSTMAN is run in the web browser which is the biggest difference. That said, I disabled csrf for the entire class RoleList but one identical request works with Curl, but fails with POSTMAN.

    0 讨论(0)
提交回复
热议问题