在 Google App Engine Python API 中获取原始发布数据

发布于 2024-08-21 11:22:01 字数 1784 浏览 1 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

往昔成烟 2024-08-28 11:22:01

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, use self.request.body.

野の 2024-08-28 11:22:01

尝试使用 application/x-www-form-urlencoded 以外的内容类型提交 POST 数据,这是浏览器提交表单时的默认类型。如果您使用不同的内容类型,原始帖子数据将位于 self.request.body 中,如 Wooble 建议的那样。

如果这实际上来自 HTML 表单,您可以将 enctype 属性添加到

元素来更改浏览器使用的编码。尝试类似 enctype="application/octet-stream" 的内容。

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 in self.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 like enctype="application/octet-stream".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文