iPhone发布到Django并获得CSRF验证失败 [英] iPhone POSTing to Django and gets CSRF verification failed

查看:146
本文介绍了iPhone发布到Django并获得CSRF验证失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发送一个POST请求从IPhone到Django,并得到CSRF验证失败,我不能完全理解。
我试图通过互联网找到一个很好的解决方案,但我不能。
有没有简单的方法来发送到django?



这是我的代码:

  NSString * post = [NSString stringWithFormat:@s = aaa& r = k& c = gg]; 
NSData * postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSString * postLength = [NSString stringWithFormat:@%d,[postData length]];
NSMutableURLRequest * request = [[NSMutableURLRequest alloc] init];
[请求setURL:[NSURL URLWithString:(@http:// localhost:8000 / messages / views /)]];
[request setHTTPMethod:@POST];
[request setValue:postLength forHTTPHeaderField:@Content-Length];
[request setValue:@application / x-www-form-urlencoded charset = utf-8forHTTPHeaderField:@Content-Type];
[请求setHTTPBody:postData];
NSError *错误;
NSURLResponse * response;
NSData * urlData = [NSURLConnection sendSynchronousRequest:request returnedResponse:& response error:& error];
NSString * data = [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(data);


解决方案

通常这归结为正确设置头。已经详细说明了答案。您需要的相关部分是:

  xhr.setRequestHeader(X-CSRFToken,令牌)

有关从Cookie获取令牌的详细信息,请参阅链接的答案,为简洁起见,我没有将其从那里复制。我真的不知道你的代码的上下文,所以这种检索方法可能不会直接应用。无论如何,您需要以某种方式获取令牌。



当您拥有令牌时,请向 NSMutableURLRequest 中添加标题。发布请求时,错误应该已经没了。

  [request addValue:token forHTTPHeaderField:@X-CSRFToken]; 


I send a POST request from IPhone to Django and get "CSRF verification failed", which I can't perfectly understand. I tried to find a good solution over the internet, but I couldn't . is there any simple way to POST to django?

this is my code:

   NSString *post =[NSString stringWithFormat:@"s=aaa&r=k&c=gg"];
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:(@"http://localhost:8000/messages/views/")]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(data);

解决方案

Normally this boils down to setting the headers correctly. There is an answer that already details this. The relevant part you need is this:

xhr.setRequestHeader("X-CSRFToken", token)

See the linked answer for details on getting the token from the cookies, for brevity I didn't copy it from there. I don't really know the context of your code, so this method of retrieval might not directly apply. Regardless, you need to get the token somehow.

When you have the token, add a header to the NSMutableURLRequest. Upon posting the request, the error should be gone.

[request addValue:token forHTTPHeaderField:@"X-CSRFToken"];

这篇关于iPhone发布到Django并获得CSRF验证失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆