在 Google App Engine Python API 中获取原始发布数据
我正在尝试使用 self.request.get('content') 将原始数据作为帖子发送到 Google App 引擎,但徒劳无功。它返回空。我确信数据是从客户端发送的,因为我检查了另一个简单的服务器代码。
知道我做错了什么吗?我在客户端使用以下代码生成 POST 调用 (objective-c/cocoa-touch)
NSMutableArray *array = [[NSMutableArray alloc] init];
NSMutableDictionary *diction = [[NSMutableDictionary alloc] init];
NSString *tempcurrentQuestion = [[NSString alloc] initWithFormat:@"%d", (questionNo+1)];
NSString *tempansweredOption = [[NSString alloc] initWithFormat:@"%d", (answeredOption)];
[diction setValue:tempcurrentQuestion forKey:@"questionNo"];
[diction setValue:tempansweredOption forKey:@"answeredOption"];
[diction setValue:country forKey:@"country"];
[array addObject:diction];
NSString *post1 = [[CJSONSerializer serializer] serializeObject:array];
NSString *post = [NSString stringWithFormat:@"json=%@", post1];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSLog(@"Length: %d", [postData length]);
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:@"http://localhost:8080/userResult/"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
questionsFlag = FALSE;
[[NSURLConnection alloc] initWithRequest:request delegate:self];
服务器端代码是:
class userResult(webapp.RequestHandler):
def __init__(self):
self.qNo = 1
def post(self):
return self.request.get('json')
I am trying to get raw data sent as post to Google App engine, using self.request.get('content'), but in vain. It returns empty. I am sure the data is being sent from the client, coz I checked with another simple server code.
Any idea what I am doing wrong? I am using the following code on the client side generating the POST call (objective-c/cocoa-touch)
NSMutableArray *array = [[NSMutableArray alloc] init];
NSMutableDictionary *diction = [[NSMutableDictionary alloc] init];
NSString *tempcurrentQuestion = [[NSString alloc] initWithFormat:@"%d", (questionNo+1)];
NSString *tempansweredOption = [[NSString alloc] initWithFormat:@"%d", (answeredOption)];
[diction setValue:tempcurrentQuestion forKey:@"questionNo"];
[diction setValue:tempansweredOption forKey:@"answeredOption"];
[diction setValue:country forKey:@"country"];
[array addObject:diction];
NSString *post1 = [[CJSONSerializer serializer] serializeObject:array];
NSString *post = [NSString stringWithFormat:@"json=%@", post1];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSLog(@"Length: %d", [postData length]);
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:@"http://localhost:8080/userResult/"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
questionsFlag = FALSE;
[[NSURLConnection alloc] initWithRequest:request delegate:self];
The server side code is:
class userResult(webapp.RequestHandler):
def __init__(self):
self.qNo = 1
def post(self):
return self.request.get('json')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
self.request.get('content') 将为您提供使用参数名称“content”发送的数据。如果您想要原始帖子数据,请使用 self.request.body。
self.request.get('content')
will give you data sent with the argument name 'content'. If you want the raw post data, useself.request.body
.尝试使用
application/x-www-form-urlencoded
以外的内容类型提交 POST 数据,这是浏览器提交表单时的默认类型。如果您使用不同的内容类型,原始帖子数据将位于 self.request.body 中,如 Wooble 建议的那样。如果这实际上来自 HTML 表单,您可以将
enctype
属性添加到Try submitting the POST data with a content type other than
application/x-www-form-urlencoded
, which is the default when a form is submitted by a browser. If you use a different content type, the raw post data will be inself.request.body
, as Wooble suggested.If this is actually coming from an HTML form, you can add the
enctype
attribute to the<form>
element to change the encoding used by the browser. Try something likeenctype="application/octet-stream"
.