views:

567

answers:

2

Hi,

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')
+1  A: 

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.

Wooble
Wooble: Thanks for the reply, but have tried that, and even that returns me an empty string.
msk
+1  A: 

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".

Will McCutchen