How to use curl with Django, csrf tokens and POST requests

前端 未结 6 2034
一个人的身影
一个人的身影 2020-12-07 13:10

I\'m using curl to test one of my Django forms. The calls I\'ve tried (with errors from each, and over multiple lines for readability):

(1):

curl
-d          


        
6条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-07 14:04

    Try:

    curl
     -d "email=test@test.com&a=1"
     http://127.0.0.1:8083/registrations/register/
    

    Notice especially the format of the -d argument.

    However, this probably won't work, as your view likely needs a POST request instead of a GET request. Since it will be modifying data, not just returning information.

    CSRF protection is only required for 'unsafe' requests (POST, PUT, DELETE). It works by checking the 'csrftoken' cookie against either the 'csrfmiddlewaretoken' form field or the 'X-CSRFToken' http header.

    So:

    curl
     -X POST
     -d "email=test@test.com&a=1&csrfmiddlewaretoken={inserttoken}"
     --cookie "csrftoken=[as above]"
     http://127.0.0.1:8083/registrations/register/
    

    It's also possible to use --header "X-CSRFToken: {token}" instead of including it in the form data.

提交回复
热议问题