2010-02-12 51 views
3

我试图使用self.request.get('content')获取原始数据作为发布到Google App引擎,但徒劳无功。它返回空。我确信数据正在从客户端发送,因此我使用另一个简单的服务器代码进行了检查。在Google App Engine中获取原始发布数据Python API

任何想法我做错了什么?我使用在客户端产生的POST调用(Objective-C的/可可触摸)下面的代码

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') 
+0

如果您发布生成请求的HTML表单以及正在接收POST的AppEngine RequestHandler,我们可能会继续做下去。 – 2010-02-12 13:48:19

回答

4

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

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

7

self.request.get('content')会给你发送的数据参数名称'content'。如果您想要原始数据,请使用self.request.body

+0

Wooble:谢谢你的回复,但已经尝试过了,甚至会给我一个空的字符串。 – msk 2010-02-12 14:07:04