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

前端 未结 6 2051
一个人的身影
一个人的身影 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:08

    Here is how i did it, using the rest framework tutorial

    open a browser e.g. chrome then pressing F12 open the developer tab and monitor the Network, login using your user credentials and get your CRSF token from monitoring the POST

    then in curl execute:

    curl http://127.0.0.1:8000/snippets/ \
     -X POST \
     -H "Content-Type: application/json" \
     -H "Accept: text/html,application/json" \
     -H "X-CSRFToken: the_token_value" \
     -H "Cookie: csrftoken=the_token_value" \
     -u your_user_name:your_password \
     -d '{"title": "first cookie post","code": "print hello world"}' 
    

    I think its cleaner to not put the token in the body but rather the header using X-CSRFToken

提交回复
热议问题