iPhone发布到Django并获得CSRF验证失败

编程入门 行业动态 更新时间:2024-10-24 06:37:26
本文介绍了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:(@"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验证失败

本文发布于:2023-10-18 13:17:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1504329.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:iPhone   Django   CSRF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!