所以我试图将表单数据发布到我同事的网站,以便从我的 iPhone 应用程序登录(简单的用户名和密码)。但是,看来我需要一个 CSRF 令牌才能发布。我已经对此进行了大量研究,并从我可以从csrftoken cookie
(我在这里读到:https ://docs.djangoproject.com/en/dev/ref/contrib/csrf/ )使用 GET获得这个令牌要求。问题是,我不知道如何处理这个 GET 请求?我从哪里得到?
到目前为止,这是我的帖子请求的代码:
NSURL *url = [NSURL URLWithString:SERVER_ADDRESS];
NSData* postData= //Some form data
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:[NSString stringWithFormat:@"%d", postData.length] forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
[request addValue:token forHTTPHeaderField:@"X-CSRFToken"]; //Where do I get this token from
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request
delegate:self];
[connection start];
我知道 StackOverflow 上有很多类似的帖子,但我没有找到任何看起来完整的答案。通常它只是将我引导到上面的链接,该链接仅填充了 AJAX 相关信息。帮助将不胜感激!