2010-02-12 49 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