【问题标题】:Getting raw post data in Google App Engine Python API在 Google App Engine Python API 中获取原始帖子数据
【发布时间】:2010-02-12 12:00:22
【问题描述】:

我正在尝试使用 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')

【问题讨论】:

  • 如果您发布生成请求的 HTML 表单以及接收 POST 的 AppEngine RequestHandler,我们可能还有其他工作要做。

标签: iphone python google-app-engine post request


【解决方案1】:

self.request.get('content') 将为您提供以参数名称“内容”发送的数据。如果您想要原始帖子数据,请使用self.request.body

【讨论】:

  • Wooble:感谢您的回复,但已经尝试过了,甚至返回一个空字符串。
【解决方案2】:

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

如果这实际上来自 HTML 表单,您可以将 enctype 属性添加到 <form> 元素以更改浏览器使用的编码。试试enctype="application/octet-stream"

【讨论】:

    猜你喜欢
    • 2011-01-01
    • 2010-11-24
    • 2011-04-28
    • 2011-02-03
    • 2015-11-06
    • 2011-05-12
    • 2013-06-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多