iphone Json POST请求到Django服务器在QueryDict中创建QueryDict [英] iphone Json POST request to Django server creates QueryDict within QueryDict
本文介绍了iphone Json POST请求到Django服务器在QueryDict中创建QueryDict的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用JSON库创建一个来自Objective C的JSON POST请求,如下所示:
I'm creating a JSON POST request from Objective C using the JSON library like so:
NSMutableURLRequest *request;
request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@/", host, action]]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json-rpc" forHTTPHeaderField:@"Content-Type"];
NSMutableDictionary *requestDictionary = [[NSMutableDictionary alloc] init];
[requestDictionary setObject:[NSString stringWithString:@"12"] forKey:@"foo"];
[requestDictionary setObject:[NSString stringWithString@"*"] forKey:@"bar"];
NSString *theBodyString = requestDictionary.JSONRepresentation;
NSData *theBodyData = [theBodyString dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:theBodyData];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
当我在Django视图中读取此请求时,调试器显示它占用整个JSON字符串,并将其作为POST QueryDict的第一个键:
When I read this request in my Django view the debugger shows it took the entire JSON string and made it the first key of the POST QueryDict:
POST QueryDict: QueryDict: {u'{"foo":"12","bar":"*"}': [u'']}> Error Could not resolve variable
我可以读取第一个键,然后使用JSON作为黑客重新分析。但是为什么JSON字符串没有正确发送?
I can read the first key and then reparse using JSON as a hack. But why is the JSON string not getting sent correctly?
推荐答案
这是使用json数据处理POST请求的方式: / p>
This is the way to process a POST request with json data:
def view_example(request):
data=simplejson.loads(request.raw_post_data)
#use the data
response = HttpResponse("OK")
response.status_code = 200
return response
这篇关于iphone Json POST请求到Django服务器在QueryDict中创建QueryDict的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文