How to get CSRF token in iOS?

后端 未结 2 765
谎友^
谎友^ 2021-01-02 11:38

So I\'m trying to POST form data to my colleague\'s site in order login (simple username and password) from my iPhone app. However, it appears that I need a CSRF Token in o

2条回答
  •  礼貌的吻别
    2021-01-02 12:07

    As pointed out in the comments you could either parse it from any page containing a form on your friend's website.

    If you want one for your own ask him to render this template at /ios/

    ios.html:

    {% csrftoken %}
    

    Then launch a GET request:2 You can parse the value of the token with a regex:

    NSString *regex = @"csrfmiddlewaretoken\".*?\"\(.*?\)\"";
    

    Finally you have to set the value of the X-CSRFToken on your following HTTP POST requests.

提交回复
热议问题